gpt4 book ai didi

java - 在 Servlet 中读取序列化对象时出现 EOFException

转载 作者:行者123 更新时间:2023-12-02 08:24:26 25 4
gpt4 key购买 nike

我有一个小程序试图将java序列化对象发送到servlet(托管在Tomcat 6中)。 URL 是正确的,并且 applet 到达了正确的 servlet,但是当读取 servlet 端的任何内容时,我收到错误。

这是小程序代码:


String adresse = "/Applic_ClientsPartners/NewMemberServlet";
URL page = getDocumentBase();
String protocole = page.getProtocol();
String host = page.getHost();
int port = page.getPort();
System.out.println(protocole + "://" + host + ":" + port + adresse);
URL serv = new URL(protocole, host, port, adresse);
URLConnection conn = serv.openConnection();
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
//conn.setRequestProperty("Content-Type", "application/x-java-serialized-object");
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setDoInput(true);
conn.setDoOutput(true);
System.out.println("Connection open");
ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
System.out.println("Envoi de 'test'"); //sending a test String
String t = "test";
oos.writeObject(t);
System.out.println("test flush");
oos.flush();
System.out.println("test between flush & writeObject");
oos.writeObject(nouveau); //this is a serializable custom object
oos.flush();
//Boolean result = ois.readBoolean();
ois.close();
oos.close();
System.out.println("Streams fermés"); //streams closed successfully

这是 servlet 代码:


response.setContentType("application/octet-stream");
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
System.out.println("AVAILABLE " + ois.available());
System.out.println("Pré read test");
String t = (String) ois.readObject();
System.out.println(t + " lu");
Cards.Member nouveau = (Cards.Member) ois.readObject();
System.out.println("Post read test");
oos.close();
ois.close();
System.out.println("Streams fermés"); //Streams closed successfully
return;

当然,我有一个围绕两者的 try ... catch(IOException ex) 。错误如下:


java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at Servlets.NewMemberServlet.doPost(NewMemberServlet.java:96)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)

我真的不明白这里发生了什么:servlet 的 ObjectInputStream 似乎什么也没得到,但我在同一个 Web 项目中有另一个 applet-servlet,尽管有一个 http 隧道,而且它们工作得很好?!

任何帮助将不胜感激:)

最佳答案

仅在将数据写入 ObjectOutputStream 后才尝试在小程序中创建 ObjectInputStream。在 servlet 端,延迟创建 ObjectOutputStream。

原因:

我认为您编写的代码应该可以工作(顺序正确),但我知道过去我在这些流设置的顺序和时间方面遇到了问题。

关于java - 在 Servlet 中读取序列化对象时出现 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com