java.io.IOException:读取序列化对象时过早的EOF

时间:2012-11-05 10:31:49

标签: java debugging serialization ioexception httpserver

我正在做一个家庭作业,我必须以序列化模式来回发送一个对象。我有一个Client3.java文件,它将一个对象发送到Server3.java.And Server3.java做同样的事情。但是当我尝试在Client3中读取对象时,他抛出java.io.IOException: Premature EOF错误。从广泛的搜索中我发现我的程序比InputStream读取运行得更快但我不知道如何解决这个问题

这里是我的2个文件

package server3;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL; 

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);
        //here we send an serialized object
        Send obj = new Send();
        ObjectOutputStream objOut = new               
                ObjectOutputStream(s.getOutputStream());
        objOut.writeObject(obj);

        /*********this is the chunk of code that makes the error*****************/
        //here we read the serialized object
        InputStream in = s.getInputStream();
        ObjectInputStream ios = new ObjectInputStream(in);
        SendResponse oin = (SendResponse) ios.readObject();
        String gabim=oin.getGabim();
        String gabimNr =oin.getGabimNr();
        System.out.println(gabim);
        System.out.println(gabimNr);
        ios.close();
        s.disconnect();
        /***************************************************************************/
        //catch all the possible errors 
      } catch (IOException ex) {
        System.err.println("Gabim ne klient"+ex);
        ex.printStackTrace();
      }
    }
}

class Send implements Serializable {
    // the object to be send to the server
    private static final long serialVersionUID = 1L;
    int id = 1;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public int getPaid() {
        return paid;
    }
    public void setPaid(int paid) {
        this.paid = paid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    int amount = 2000;
    int paid = 800;
    String name = "Andi Domi";
}

这是Server3.java文件

package server3;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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 {
    //create the server
    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 {

        /*******we start to read the object send from the client here*******/ 
        //create the sream
        ObjectInputStream ios = new ObjectInputStream(t.getRequestBody());
        //variables we need to connect to the database
        final String url = "jdbc:mysql://localhost/httpServer";
        final String user = "root";
        final String password = "";
        try {
            //create the object to be read
            Send oin = (Send) ios.readObject();
            int id = oin.getId();
            String emri = oin.getName();
            int amount = oin.getAmount();
            int paid = oin.getPaid();
            //jdbc try
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user,password);           
            //insert values into the first table
            PreparedStatement s = con
                    .prepareStatement("INSERT INTO      person(ID,Name,Amount,Paid) VALUES (?,?,?,?)");
            s.setInt(1, id);
            s.setString(2, emri);
            s.setInt(3, amount);
            s.setInt(4, paid);
            s.executeUpdate();
            //read the values from a table and add them to an array list
            ResultSet rs = s.executeQuery("SELECT * from personat ORDER BY ID");
            ArrayList<Personat> perList = new ArrayList<Personat>();
            if (rs != null) {
                while (rs.next()) {
                    Personat per = new Personat();
                    per.setID(rs.getInt("ID"));
                    per.setName(rs.getString("Name"));
                    per.setAmount(rs.getInt("Amount"));
                    perList.add(per);
                }
            }
            //send the serialized object
            String gabim="Ska asnje gabim";
            String gabimNr="1";
            t.sendResponseHeaders(200,0);
            OutputStream os = t.getResponseBody();
            SendResponse obj = new SendResponse(gabim,gabimNr,perList);
            ObjectOutputStream objOut = new ObjectOutputStream(os);
            objOut.writeObject(obj);
            objOut.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            //ketu cojme nje object me keto te dhena
            String gabim=("Dicka nuk shkoi mire me marrjen e objektit");
            String gabimNr="3";
            rrayList<Personat> perList = null;
            t.sendResponseHeaders(200,0);
            OutputStream os = t.getResponseBody();
            SendResponse obj = new SendResponse(gabim,gabimNr,perList);
            ObjectOutputStream objOut = new ObjectOutputStream(os);
            objOut.writeObject(obj);
            objOut.close();
        } catch (SQLException e) {
            //ketu cojme nje objekt me keto te dhena
            e.printStackTrace();
            String gabim=("Dicka nuk shkoi mire ne lidhje me databasin");
            String gabimNr="4";
            ArrayList<Personat> perList = null;
            t.sendResponseHeaders(200,0);
            OutputStream os = t.getResponseBody();
            SendResponse obj = new SendResponse(gabim,gabimNr,perList);
            ObjectOutputStream objOut = new ObjectOutputStream(os);
            objOut.writeObject(obj);
            objOut.close();
        }
    }
}
}

class Personat {
int ID;
String Name;
int Amount;
public int getID() {
    return ID;
}
public void setID(int iD) {
    ID = iD;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
}
public int getAmount() {
    return Amount;
}
public void setAmount(int amount) {
    Amount = amount;
}
}

class SendResponse implements Serializable {
private static final long serialVersionUID = 1L;

String gabim;
String gabimNr;
ArrayList<Personat> personList;
public SendResponse(String gabim, String gabimNr,
        ArrayList<Personat> personList) {
    super();
    this.gabim = gabim;
    this.gabimNr = gabimNr;
    this.personList = personList;
}
public String getGabim() {
    return gabim;
}
public void setGabim(String gabim) {
    this.gabim = gabim;
}
public String getGabimNr() {
    return gabimNr;
}
public void setGabimNr(String gabimNr) {
    this.gabimNr = gabimNr;
}
public ArrayList<Personat> getPersonList() {
    return personList;
}
public void setPersonList(ArrayList<Personat> personList) {
    this.personList = personList;
}
} 

从Client3文件中的ex.printStackTrace();运行后,我得到了这个:

java.io.IOException: Premature EOF
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
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 server3.Client3.main(Client3.java:29)

我不知道热得更快阅读文件或让我的程序变慢,有人有任何想法吗?

1 个答案:

答案 0 :(得分:0)

我认为这不是由于程序的速度,如果没有任何东西可以阅读,我希望readObject()能够阻止。相反,我认为您在尝试阅读的数据中缺少终结符,您需要查看发送到您的程序的数据。类似的问题已解决here,但它使用的是readLine()