2011-05-17 70 views
2

我正在從Java本地應用程序上傳一個包含文件到服務器的對象。我的計劃是在tomcat上運行的servlet將使用doGet方法中的ObjectInputStream獲取對象。但我得到一個EOFE xception`。將序列化數據發送到servlet時發生java.io.EOFException

下面是客戶端代碼

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

回答

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網站。

+0

確定i'l檢查這些阿帕奇包。 btw連接命令在哪裏消失? 我試着在幾個地方添加它,並且我在java.io.ObjectInputStream處獲得了 $ PeekInputStream.readFully(Unknown Source) – 2011-05-17 20:52:53

+0

您不需要它。你也開了太早。 'getInputStream()'會在適當的時候隱式地運行'connect()'。只需按照我的回答完全使用代碼即可。在你原來的客戶端代碼中,你需要用'out.close()'替換'URL url'。這正是你所需要的,並且已經以正確的順序。 – BalusC 2011-05-17 21:00:28

+0

要了解更多關於使用'URLConnection'的信息,請查看這篇文章http:// stackoverflow。com/questions/2793150/how-to-use-java-net-urlconnection-fire-and-handle-http-requests – BalusC 2011-05-17 21:01:32

1

我解決了這個PBM與客戶端:

HttpURLConnection cnx = (HttpURLConnection) new URL("http://localhost:8080/web").openConnection(); 
cnx.setRequestMethod("PUT"); 
相關問題