私はシリアライズモードでオブジェクトを前後に送信しなければならない宿題に取り組んでいます.Server3.javaにオブジェクトを送信するClient3.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)
ファイルをより速く読み取るか、プログラムを遅くするかを知りません。誰かアイデアがありますか?