2

ファイルを含むオブジェクトを Java ローカル アプリケーションからサーバーにアップロードしようとしています。私の計画は、Tomcat で実行されているサーブレットがメソッドObjectInputStream内のを使用してオブジェクトを取得することdoGetです。しかし、私は例外を取得しEOFEます。

クライアントコードはこちら

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

ここにサーバーのサーブレットがあります

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)

この質問を見ましたが、助けにはなりませんでした。私はTomcat 7を使用しています。

4

2 に答える 2

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

URLConnection#getInputStream()、応答本文を取得するために、HTTP 要求をサーバーにすぐに送信します。コードを書いたように、これは HTTP リクエスト本文に少しでも書き込む前に行われます。したがって、EOFExceptionサーバー側で。

必要なデータを HTTP 要求本文に書き込んだURLConnection#getInputStream() 、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);

doPost()また、基本的には HTTP POST リクエストを送信しているため、これはメソッドではなくサーブレットのメソッドで処理する必要がありますdoGet()


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

于 2011-05-17T20:29:43.450 に答える
1

クライアント側でこのpbmを解決しました:

HttpURLConnection cnx = (HttpURLConnection) new URL("http://localhost:8080/web").openConnection();
cnx.setRequestMethod("PUT");
于 2012-03-27T13:19:56.473 に答える