我现在需要服务端去查询数据库,得到一个Enumeration<>对象,然后该对象通过objetctOutputStream的对象传给客户端,为了序列化,我写了一个NetTransfer类。代码如下
public class NetTransfer implements Serializable {
Object obj;
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
服务端代码如下:
public static void getAllUser()
{
try
{
//服务器开始在数据库查询
Vector<User> userVec=new Vector<User>();
rSet=stmt.executeQuery("select * from userdata");
while(rSet.next())
{
//以下只是我的程序中有不同类型的人,此处为多态
switch (rSet.getString("role")) {
case "administrator":
userVec.add(new Administrator(rSet.getString("name"), rSet.getString("password"), rSet.getString("role")));
break;
case "operator":
userVec.add(new Operator(rSet.getString("name"), rSet.getString("password"), rSet.getString("role")));
break;
case "browser":
userVec.add(new Browser(rSet.getString("name"), rSet.getString("password"), rSet.getString("role")));
break;
default:
break;
}
}
Enumeration<User> e = userVec.elements();
NetTransfer allUsers=new NetTransfer();
allUsers.setObj(e);
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
/*异常提示此处没序列化*/ oos.writeObject(allUsers);
oos.flush();
//oos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
客户端
Enumeration<User> allUsers=null;
//服务器返回的信息
ObjectInputStream ois=new ObjectInputStream(clientSocket.getInputStream());
NetTransfer allUsersTransfer=new NetTransfer();
allUsersTransfer=(NetTransfer)ois.readObject();
allUsers=(Enumeration<User>)allUsersTransfer.getObj();
//ois.close();
} catch (Exception e) {
e.printStackTrace();
return allUsers;
}
return allUsers;
}
对序列化不是很懂,求助