我正在尝试在jsp中读取xml并通过网络将其作为char []传递给applet但我得到了 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;
}
}
答案 0 :(得分:0)
您需要为服务器上的响应设置上下文类型为“text / xml”。
答案 1 :(得分:0)
您的代码中存在许多问题:
http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria
,您不应在连接的输出流中发送任何内容。您应该了解HTTP的工作原理。