gpt4 book ai didi

将序列化数据发送到 servlet 时出现 java.io.EOFException

转载 作者:行者123 更新时间:2023-12-04 06:25:58 26 4
gpt4 key购买 nike

我想从我的 Java 本地应用程序上传一个包含文件到服务器的对象。我的计划是在 tomcat 上运行的 servlet 将使用 ObjectInputStream 获取对象。在 doGet方法。但我得到了一个 EOFE xception`。

这是客户端代码

import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("c:\\rafi.txt");
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
inputStream.close();
File2 c2 = new File2(buffer);
URL url = new URL("http://localhost:8080/servertest/Server");
URLConnection cnx = url.openConnection();
cnx.setDoInput(true);
cnx.setDoOutput(true);
cnx.setRequestProperty("Content-Type", "application/octet-stream");
InputStream in = cnx.getInputStream();
OutputStream out = cnx.getOutputStream();
cnx.connect();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(c2);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(in);
boolean readBoolean = ois.readBoolean();
System.out.println(readBoolean);
ois.close();
in.close();
out.close();
}
}

这是服务器的 servlet
import java.io.*;
import javax.servlet.*;
@WebServlet("/Server")
public class Server extends HttpServlet {
private static final long serialVersionUID = 1L;
public Server() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
InputStream in = req.getInputStream();
OutputStream out = res.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
ObjectInputStream ois = new ObjectInputStream(in);

File2 data_in;
try {
data_in = (File2) ois.readObject();
byte[] a = new byte[data_in.mybytearray.length];
System.arraycopy(data_in.mybytearray, 0, a, 0,data_in.mybytearray.length);
System.out.println(a.toString());
oos.writeBoolean(true);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
oos.writeBoolean(false);
}
finally{
oos.close();
}

res.setContentType("java-internal/" + File2.class.getName());
in.close();
}
}

当我调试服务器端并运行客户端时,我在这一行收到异常
ObjectOutputStream oos = new ObjectOutputStream(out);

这是我得到的错误
SEVERE: Servlet.service() for servlet [test1.Server] in context with path [/servertest] threw exception
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at test1.Server.doGet(Server.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我看到这个 question ,但我没有帮助我。我正在使用 tomcat 7。

最佳答案

InputStream in = cnx.getInputStream();
OutputStream out = cnx.getOutputStream();
URLConnection#getInputStream()将立即将 HTTP 请求发送到服务器以检索响应正文。按照您编写代码的方式,这将在您向 HTTP 请求正文写入任何内容之前发生。因此 EOFException在服务器端。

您需要通过 URLConnection#getInputStream() 请求 HTTP 响应正文 您已将必要的数据写入 HTTP 请求正文。这是一个重写:
URLConnection connection = new URL("http://localhost:8080/servertest/Server").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
oos.writeObject(c2);
oos.close();

ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
boolean readBoolean = ois.readBoolean();
ois.close();

System.out.println(readBoolean);

此外,由于您基本上是在发送 HTTP POST 请求,因此您需要在 servlet 的 doPost() 中处理它。方法而不是 doGet()方法。

无关 具体问题:这并不是通过 HTTP 发送文件的最佳方式。虽然它可能有效,但它与 Java 序列化机制非常紧密地结合在一起。我建议发送 HTTP multipart/form-data请求代替。这可以通过 Apache HttpComponents Client 实现在客户端和 Apache Commons FileUpload在服务器端。这样,servlet 可重用于其他目的,例如带有 <input type="file"> 的 HTML 表单。在前面。客户端也可以通过这种方式重新用于将文件上传到其他 HTTP 网站。

关于将序列化数据发送到 servlet 时出现 java.io.EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036622/

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