我使用 序列化对象(ByteArrayOut/InputStream;ObjectOut/InputStream)
public static byte[]objectToByte(Object object)
{
byte[] bytes = null;
try {
//object to bytearray
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(object); bytes = bo.toByteArray(); bo.close();
oo.close();
}
catch(Exception e) {
e.printStackTrace();
}
return bytes;
}public static Object byteToObject(byte[] bytes) throws Exception
{
java.lang.Object obj = null;
//bytearray to object
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close();
oi.close();
return obj; }我的类public class Message implements Serializable{
String sendName;
String receiveName;
int command;
Date sendDate;
Object content;
public Message()
{
}
public Message(String inSendName,String inReceiveName,int inCommand,Date inSendDate,Object inContent)
{
this.setSendName(inSendName);
this.setReceiveName(inReceiveName);
this.setCommand(inCommand);
this.setSendDate(inSendDate);
this.setContent(inContent);
}//后面get,set就不贴了
这样序列出来的对象是带包的路径的,服务端和客户端的类对象的包路径一定要一样才能反序列出来,我该怎么弄才能不带路径的序列化啊?或者说有其他方法在网络里传递对象。我的网络传输通道必须是InputStream 和OutputStream;不能使用对象流
解决方案 »
- Servlet里面跳转JSP页面报错。大家帮忙看一下
- 刚开始学struts2就被人警告了
- new Integer() 跟Integer.valueof()有什么区别,用哪个好?
- EJB3更新问题
- 菜鸟求助关闭的语句
- 单位要用NIO开发手机游戏服务器,我该准备哪些资料?
- hibernate delete 的问题
- 我有一套某国内软件公司的一套项目开发实施的详细设计(共一百三十页),愿与人交换类似资料.
- weblogic的jndi的问题"Cannot instantiate class:weblogic.jndi.WLInitialContextFactory "
- 求救:j2ee高手帮我看看
- 程序转换问题
- 怎样将web项目放到互联网上
Message类所在包:com.myclass服务端一:Message类所在包:com.other 通讯结果:不能反序列化对象服务端二Message类所在包:com.myclass 通讯结果:可以反序列成对象结论:只要客户端和服务端类所在包路径相同就可以反序列。
客户端:
Message类所在包:com.myclass服务端一:Message类所在包:com.other 通讯结果:不能反序列化对象
================================包名不一致,只是Message类一致,在JVM中,类加载器识别为不用的class.所以不能反序列化对象