java.io.IOException: Premature EOF while reading serialized object

I am working on a homework which i have to send back and forth an object in serialized mode.I have a Client3.java file which sends an object to Server3.java.And Server3.java do the exact same thing.But when i try to read the object in Client3 he throws a `java.io.IOException: Premature EOF` error.From an extensive search i found that my program runs faster than the InputStream reading but i dont know how to fix this here my 2 files 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"; } And here it is the Server3.java file 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 perList = new ArrayList(); 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 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 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 personList; public SendResponse(String gabim, String gabimNr, ArrayList 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 getPersonList() { return personList; } public void setPersonList(ArrayList personList) { this.personList = personList; } } after i run it from the `ex.printStackTrace();` in the Client3 file i get this : 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.(Unknown Source) at server3.Client3.main(Client3.java:29) i dont know hot to read the file faster or make my program slower ,do someone have any idea ?

以上就是java.io.IOException: Premature EOF while reading serialized object的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏