從這裏先前的問題繼續。嘗試通過HTTP發送此對象Employee。我沒有收到任何錯誤,但希望在另一端打印出員工詳細信息,但沒有發生任何事情。我打開我的日誌文件以查看我的tomcat服務器上的打印輸出,但除了表示該方法已開始顯示START打印輸出,我沒有收到END之外。所以在這一部分,某些東西並不適用。沒有錯誤,但對象通過HTTP不工作?
下面是測試類員工:
public class Employee implements java.io.Serializable {
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}
客戶端:
公共類SerializeAndSend {
public static void main(String args[]){
one.Employee e = new one.Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
sendObject(e);
}
public static Object sendObject(Object obj) {
URLConnection conn = null;
Object reply = null;
try {
// open URL connection
URL url = new URL("///myURL///");
conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
// send object
ObjectOutputStream objOut = new ObjectOutputStream(conn.getOutputStream());
objOut.writeObject(obj);
objOut.flush();
objOut.close();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
// recieve reply
try {
ObjectInputStream objIn = new ObjectInputStream(conn.getInputStream());
reply = objIn.readObject();
objIn.close();
} catch (Exception ex) {
// it is ok if we get an exception here
// that means that there is no object being returned
System.out.println("No Object Returned");
if (!(ex instanceof EOFException))
ex.printStackTrace();
System.err.println("*");
}
return reply;
}
}
我認爲是正確的。但是我卡在服務器端,我在服務器端太Employee類:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("START");
Object obj;
Employee emp = null;
ObjectInputStream objIn = new ObjectInputStream(req.getInputStream());
try {
obj = objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
emp = (Employee)objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("END");
System.out.println(emp.name);
}
任何想法什麼在接收端的問題呢?
可能的重複[序列化通過HTTP正確的方式來轉換對象。](http://stackoverflow.com/questions/14969461/serializing-over-http-correct-way-to-convert-object) – EJP 2013-02-20 05:33:49