本文介绍了使用HttpHandler对象序列化的java.io.EOFException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有2个文件,$ code> 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); //创建一个默认的执行器
server.start();
}

静态类MyHandler实现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实现Serializable {
private static final long serialVersionUID = 1L;
int ID = 3;
String Name =Andi;
}

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

但是当我运行它时显示我

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

我不明白为什么。

解决方案

问题是您正在尝试将字符串响应和对象放入 response.length()字节。发生什么是只发送 response.length()字节,所以如果你尝试阅读更多,你会得到 EOFException



如果您将 responseLength 参数设置为0,则允许您传输任意数量的数据

  t.sendResponseHeaders(200,0); 

如果要在其中写入更多数据,您也不应该关闭流。在所有的写作完成之前,不要调用 os.close()


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.

解决方案

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.

这篇关于使用HttpHandler对象序列化的java.io.EOFException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:37