0

我正在做一个作业,我必须以序列化模式来回发送一个对象。我有一个 Client3.java 文件,它将一个对象发送到 Server3.java。而 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;
}
} 

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

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)

我不知道如何更快地读取文件或让我的程序变慢,有人知道吗?

4

1 回答 1

0

我不认为这是由于您的程序的速度,如果没有要读取的内容,我希望 readObject() 会阻塞。相反,我认为您在尝试读取的数据中缺少终止符,您需要查看发送到程序的数据。一个类似的问题在这里得到了解决,虽然它使用的是 readLine()

于 2012-11-05T10:54:06.790 回答