我有一个简单的java helloworld applet和一个带有引用该applet的标记的html文件。当我使用apache服务器时,整个过程正常。客户端计算机访问我服务器上的端口80,输入带有applet标记的html并打开applet。
但是,如果我尝试通过其他端口通过Java套接字发送这个相同的html文件,以便客户端从浏览器打开它,它不起作用,我在客户端浏览器上收到applet错误。
这是接受套接字连接时发送文件的方式:
DataOutputStream out=null;
File f=new File("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\index.html");
DataInputStream htmlFile;
try {
out = new DataOutputStream(clientSocket.getOutputStream());
htmlFile = new DataInputStream(new FileInputStream(f));
int len = (int) f.length();
byte[] buf = new byte[len];
htmlFile.readFully(buf);
htmlFile.close();
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Content-Length: " + f.length() +"\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.write(buf);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我得到的错误:
Java Plug-in 1.6.0_30
Using JRE version 1.6.0_30-b12 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Administrator
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.plugin.util.GrayBoxPainter.setProgressFilter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.setupGrayBoxPainter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.access$700(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
exception: name.
java.lang.IllegalArgumentException: name
at sun.plugin2.applet.Applet2ClassLoader.addJar(Unknown Source)
at sun.plugin2.applet.Applet2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.IllegalArgumentException: name
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.plugin.util.GrayBoxPainter.setProgressFilter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.setupGrayBoxPainter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.access$700(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
exception: name.
java.lang.IllegalArgumentException: name
at sun.plugin2.applet.Applet2ClassLoader.addJar(Unknown Source)
at sun.plugin2.applet.Applet2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.IllegalArgumentException: name
java.lang.ClassFormatError: Incompatible magic value 1013018736 in class file MyApplet/HelloWorld
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassFormatError: Incompatible magic value 1013018736 in class file MyApplet/HelloWorld
如果有人能告诉我我要做什么或其他方式,我会很感激。
编辑:
html源代码(它只是aapplet标签):
applet code =&#34; MyApplet.HelloWorld&#34; archive =&#34; HelloWorldApplet.jar&#34;,width = 300,height = 300 /
客户端应输入服务器IP地址,然后输入&#34;:portNo。&#34;进入浏览器。 &#34; 192.168.1.100:4444&#34;
是的,我知道这一点。不是applet标签的用途。我的jar文件标记在html文件中。如果用户打开html,则应下载jar文件并在客户端浏览器中显示。不知道它是如何运作的吗?
我在Android上尝试了一些东西。我希望能够打开我的android在同一个wifi网络上的计算机上发送的applet。这看起来像是这样做的方式。在Android上安装一些Web服务器似乎是一种不必要的复杂功能,如果可能的话。
答案 0 :(得分:0)
可能涉及的对象。
管理解决这个问题。感谢Nizet指出我正确的方向,并没有真正费心去看看http如何工作到现在为止。所以最后要说的是模拟http如何处理java applet。通过查看加载普通applet的请求和响应,我发现在浏览器加载html文件后,它会发送一个对引用的jar文件的请求( GET /HelloWorldApplet.jar HTTP / 1.1 然后我必须在这样的http响应中返回:
HTTP/1.1 200 OK
Content-Length: fileLength
Content-Type: application/java-archive
Then send jar file as byte buffer.
以下是全班:
public class MyThread extends Thread{
Socket clientSocket;
public MyThread(Socket socket){
this.clientSocket=socket;
}
public void run(){
System.out.println("Connection established!");
DataOutputStream out=null;
File h=new File("index.html");
File j=new File("HelloWorldApplet.jar");
DataInputStream htmlFile;
DataInputStream jarFile;
BufferedReader br=null;
try {
br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new DataOutputStream(clientSocket.getOutputStream());
htmlFile = new DataInputStream(new FileInputStream(h));
jarFile=new DataInputStream(new FileInputStream(j));
int hlen = (int) h.length();
int jlen=(int) j.length();
byte[] hbuf = new byte[hlen];
byte[] jbuf = new byte[jlen];
htmlFile.readFully(hbuf);
jarFile.readFully(jbuf);
htmlFile.close();
jarFile.close();
String request=br.readLine();
System.out.println(request);
if(request.startsWith("GET / HTTP/1.1")){
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Content-Length: " + h.length() + "\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.write(hbuf);
}
if(request.startsWith("GET /HelloWorldApplet.jar HTTP/1.1")){
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Content-Length: " + j.length() + "\r\n");
out.writeBytes("Content-Type: application/java-archive\r\n\r\n");
out.write(jbuf);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(NullPointerException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}