java.io.EOFException error on object serialization with HttpHandler

我怕爱的太早我们不能终老 提交于 2019-12-24 02:21:43

问题


I am trying to serialize an object in a HttpHandler class.

I have 2 files, Server3.java:

package server3;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Server3 {
public static void main(String[] args) throws Exception {

HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}

static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
        String response = "Kjo eshte nje pergjigje nga serveri! n";

        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();

        Personat obj = new Personat();
        ObjectOutputStream objOut = new     ObjectOutputStream(t.getResponseBody());
        objOut.writeObject(obj);
        objOut.close();
}
}
}
class Personat implements Serializable{
private static final long serialVersionUID = 1L;
int ID=3;
String Name="Andi";
}

and Client3.java:

package server3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//te gjithe personat qe jan ne database me nej objekt 
public class Client3 {
public static void main(String[] args) throws Exception {
    try {
        URL url = new URL("http://localhost:3333");
        HttpURLConnection s = (HttpURLConnection) url.openConnection();
        s.setDoOutput(true);
        s.setDoInput(true);
        s.setRequestMethod("POST");
        s.setUseCaches(false);

        InputStream in = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        int c;
        while ((c = br.read()) != -1) {
            System.out.print((char) c);
        }
        ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
        Personat oin = (Personat) ios.readObject();
        String emri=oin.Name;
        System.out.println(emri);
        ios.close();
        s.disconnect();
    } catch (IOException ex) {
        System.err.println(ex);
        System.out.print(ex);
    }
}
  }

But when I run it eclipse shows me

java.io.EOFException Kjo eshte nje pergjigje nga serveri! njava.io.EOFException`

and I cant understand why.


回答1:


The problem is that you are trying to fit both the string response and the object into response.length() bytes. What happens is that only response.length() bytes are sent and so if you try to read more you get the EOFException.

If you instead set the responseLength parameter to be 0 it will allow you to transmit an arbitrary amount of data

t.sendResponseHeaders(200, 0); 

You also shouldn't close the stream if you are going to write more data into it. Don't call os.close() until all the writing is complete.




回答2:


From java doc EOFException

Signals that an end of file or end of stream has been reached unexpectedly during input.

This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.

EOFException is a subclass of IOException. It will be thrown if you attempt to read from the stream and there is no more data to be read .

I think in InputStream there is nothing left after reading following:

InputStream in = s.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
int c;
while ((c = br.read()) != -1) {
    System.out.print((char) c);
 }


来源:https://stackoverflow.com/questions/13194755/java-io-eofexception-error-on-object-serialization-with-httphandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!