我想从我的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。
答案 0 :(得分:2)
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网站。
答案 1 :(得分:1)
我在客户端解决了这个问题:
HttpURLConnection cnx = (HttpURLConnection) new URL("http://localhost:8080/web").openConnection();
cnx.setRequestMethod("PUT");