小程序 Servlet 通信

发布于 2024-11-30 21:50:14 字数 3315 浏览 1 评论 0原文

我正在尝试读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-12-07 21:50:14

您的代码中存在很多问题:

  • 您正在将 HTML 页面内的 XML 发送到客户端。 XML 周围不应该有 html 标记。否则它就不再是 XML 了。
  • 您在 JSP 中使用 scriptlet。 JSP 应该用于生成标记,而不是其他。将读取请求参数、读取文件等的代码放在servlet中,在servlet中可以正确编写、解析、重构和设计Java代码,而不是放在JSP中。 JSP 应该包含标记、JSP 标签和 EL 表达式。不是 Java 代码。
  • 您正在使用 platforme 默认编码来读取 XML 文件,该文件可能以其他编码进行编码。您应该以字节形式读取 XML 文件,并将其发送到响应 OutputStream。 XML 文件定义了它的编码,因此接收方的 XML 解析器可以使用适当的编码将字节流转换为 XML 文档。
  • 您没有正确读取该文件。对 fis.read 的单次调用并不能保证整个文件已被读取。阅读您正在使用的方法的 javadoc,并阅读有关 IO 的 Java 教程。
  • 您正在使用 ObjectOutputStream 发送 HTTP 请求参数。 ObjectOutputStream 用于发送序列化对象。它不用于发送 HTTP 参数。 URL 应为 http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria,并且您不应在连接的输出流中发送任何内容。您应该了解 HTTP 的工作原理。
  • 从输入流(在小程序中)读取的代码也是错误的。阅读有关 IO 的 Java 教程。
  • 再次,在小程序中,您使用默认平台编码将字节数组转换为字符串。使用 XML 解析器来读取 XML。

You have many many problems in your code:

  • You're sending XML inside an HTML page to your client. You shouldn't have html markup around your XML. Else it's not XML anymore.
  • you're using scriptlets in your JSPs. JSPs should be used to generate markup, and nothing else. Put the code that reads request parameters, reads a file, etc. in a servlet, where Java code can be properly written, parsed, refactored and designed rather than in JSPs. JSPs should contain markup, JSP tags and EL expressions. Not Java code.
  • You're using the platforme default encoding to read an XML file, which is perhaps encoded in some other encoding. You should read the XML file as bytes, and send it to the response OutputStream. An XML file defines its encoding, so the receiver's XML parser can use the appropriate encoding to transform the stream of bytes into an XML document
  • You don't read the file properly. A single call to fis.read doesn't guarantee that the whole file has been read. Read the javadoc of the methods you're using, and rea the Java tutorial about IO.
  • You're using an ObjectOutputStream to send an HTTP request parameter. An ObjectOutputStream is used to send serialized objects. It's not used to send HTTP parameters. The URL should be 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.
  • the code which reads from the inputstream (in the applet) is also wrong. Read the Java tutorial about IO.
  • once again, in the applet, you're using the default platform encoding to transform a byte array into a String. Use an XML parser to read your XML.
等风来 2024-12-07 21:50:14

您需要将服务器上的响应的上下文类型设置为“text/xml”。

You need to set the context type to 'text/xml' for the response on the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文