2011-05-17 12 views
2

Javaローカルアプリケーションからサーバにファイルを含むオブジェクトをアップロードするのは難しいです。私の計画は、tomcatで実行されているサーブレットがdoGetメソッドのObjectInputStreamを使ってオブジェクトを取得するということです。しかし私はEOFE xception`を得る。私は、サーバー側のデバッグ、私はこの行で例外を取得クライアントを実行すると、ここで サーブレットにシリアル化されたデータを送信するときのjava.io.EOFException

は、サーバーのサーブレット

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(); 
    } 
} 

クライアントコードここ

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(); 
    } 
} 

されている

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を見ましたが、私は助けてくれませんでした。私はすぐにレスポンスボディを取得するためにサーバーにHTTPリクエストを送信しますTomcatの7

答えて

2
InputStream in = cnx.getInputStream(); 
OutputStream out = cnx.getOutputStream(); 

URLConnection#getInputStream()を使用しています。あなたがコードを書いたように、これはの前に行われ、の前にHTTPリクエスト本文にビットを書きました。したがって、サーバー側のEOFException

HTTPリクエスト本文にの後にHTTP応答本文をURLConnection#getInputStream()で尋ねる必要があります。ここでリライトがあります。また、

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リクエストを送信しているので、あなたは、サーブレットのdoPost()方法ではなく、doGet()方法でこれを処理する必要があります。


無関係具体的な問題へ:これは本当にHTTP経由でファイルを送信する最良の方法ではありません。これはうまくいくかもしれませんが、これはJavaのシリアライゼーションメカニズムと密接に結びついています。 HTTP multipart/form-dataリクエストを代わりに送信することをお勧めします。これは、クライアント側のApache HttpComponents Clientとサーバー側のApache Commons FileUploadによって達成できます。このようにサーブレットは、他の目的のために再利用可能です(例えば、正面に<input type="file">というHTMLフォームなど)。また、クライアントはこの方法で他のHTTP Webサイトにファイルをアップロードすることができます。

+0

をOK i'lそれらのapacheのパックをご確認ください。 btw connectコマンドはどこで消えましたか? いくつかの場所で追加しようとしましたが、私は をjava.io.ObjectInputStream $ PeekInputStream.readFully(Unknown Source) –

+0

に持っています。あなたはあまりにも早くそれを発射していました。 'getInputStream()'は、すぐに 'connect()'を暗黙的に実行します。私の答えとまったく同じようにコードを使用してください。オリジナルのクライアントコードでは、 'URL url'をこの部分と' out.close() 'に置き換える必要があります。それはまさにあなたが必要としているものであり、すでに正しい順序であります。 – BalusC

+0

'URLConnection'の使い方の詳細については、この投稿http:// stackoverflowをチェックしてください。com/questions/2793150 /使い方 - java-net-urlconnection-fire-and-handle-http-requests – BalusC

1

私は、クライアント側にして、このPBMを解く:

HttpURLConnection cnx = (HttpURLConnection) new URL("http://localhost:8080/web").openConnection(); 
cnx.setRequestMethod("PUT"); 
関連する問題