小程序 Servlet 通信
我正在尝试读取 jsp 中的 xml 并通过网络将其作为 char[] 传递给小程序,但我得到了 java.io.StreamCorruptedException:无效的流头:3C3F786D
我的jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %>
<%@ page import = "java.io.*" %>
<%@ page trimDirectiveWhitespaces="true" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<% String xmlname=(String)request.getAttribute("xmlname");
int ch;
System.out.println("the value of the xml is "+xmlname);
String filepath="C:/Users/ashutosh_k/idoc/docRuleTool/WebContent/data/Malaria.xml";
FileReader fis = new FileReader(new File(filepath));
char bin[] = new char[(int) new File(filepath).length()];
fis.read(bin);
response.getWriter().write(bin);
fis.close();
%>
</body>
</html>
我的小程序代码:
package com.vaannila.utility;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import prefuse.util.ui.JPrefuseApplet;
public class dynamicTreeApplet extends JPrefuseApplet {
private static final long serialVersionUID = 1L;
public static int i = 1;
public void init() {
System.out.println("the value of i is " + i);
URL url = null;
try {
url = new URL("http://localhost:8080/docRuleTool/XmlResponseReading.jsp");
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
//con.setRequestProperty("Content-TYpe", "application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());
oos.writeObject("Malaria");
oos.flush();
oos.close();
InputStream ois = con.getInputStream();
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (true) {
byte b[] = new byte[1024];
int retval = ois.read(b);
if (retval < b.length) {
if (retval > 0) {
byte b1[] = new byte[retval];
System.arraycopy(b, 0, b1, 0, retval);
ois.read(b1);
System.out.println(new String(b1));
}
break;
} else {
ois.read(b);
System.out.println(new String(b));
}
}
// ByteArrayInputStream bis = new ByteArrayInputStream(ois.toByteArray());
this.setContentPane(dynamicView.demo(ois, "name"));
ois.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException f) {
f.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
++i;
}
}
I am trying to read a xml in the jsp and pass the same over network as char[] to the applet but i am getting
java.io.StreamCorruptedException : invalid stream header :3C3F786D
my jsp :
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %>
<%@ page import = "java.io.*" %>
<%@ page trimDirectiveWhitespaces="true" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<% String xmlname=(String)request.getAttribute("xmlname");
int ch;
System.out.println("the value of the xml is "+xmlname);
String filepath="C:/Users/ashutosh_k/idoc/docRuleTool/WebContent/data/Malaria.xml";
FileReader fis = new FileReader(new File(filepath));
char bin[] = new char[(int) new File(filepath).length()];
fis.read(bin);
response.getWriter().write(bin);
fis.close();
%>
</body>
</html>
My applet code :
package com.vaannila.utility;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import prefuse.util.ui.JPrefuseApplet;
public class dynamicTreeApplet extends JPrefuseApplet {
private static final long serialVersionUID = 1L;
public static int i = 1;
public void init() {
System.out.println("the value of i is " + i);
URL url = null;
try {
url = new URL("http://localhost:8080/docRuleTool/XmlResponseReading.jsp");
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
//con.setRequestProperty("Content-TYpe", "application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());
oos.writeObject("Malaria");
oos.flush();
oos.close();
InputStream ois = con.getInputStream();
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (true) {
byte b[] = new byte[1024];
int retval = ois.read(b);
if (retval < b.length) {
if (retval > 0) {
byte b1[] = new byte[retval];
System.arraycopy(b, 0, b1, 0, retval);
ois.read(b1);
System.out.println(new String(b1));
}
break;
} else {
ois.read(b);
System.out.println(new String(b));
}
}
// ByteArrayInputStream bis = new ByteArrayInputStream(ois.toByteArray());
this.setContentPane(dynamicView.demo(ois, "name"));
ois.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException f) {
f.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
++i;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中存在很多问题:
http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria
,并且您不应在连接的输出流中发送任何内容。您应该了解 HTTP 的工作原理。You have many many problems in your code:
http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria
, and you shouldn't send anything in the connection's output stream. You should learn how HTTP works.您需要将服务器上的响应的上下文类型设置为“text/xml”。
You need to set the context type to 'text/xml' for the response on the server.