小弟使用socket向客户端发一个自定义对象,强制转换时出现java.io.StreamCorruptedException: Wrong format异常,不知如何解决
异常信息如下
10-23 02:20:58.687: W/System.err(1276): java.io.StreamCorruptedException: Wrong format: ac
10-23 02:20:58.689: W/System.err(1276): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701)
10-23 02:20:58.689: W/System.err(1276): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814)
10-23 02:20:58.689: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
10-23 02:20:58.689: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
10-23 02:20:58.689: W/System.err(1276): at com.wsst.utils.SocketUtil$1.run(SocketUtil.java:77)
10-23 02:20:58.689: W/System.err(1276): at java.lang.Thread.run(Thread.java:856)
10-23 02:20:58.689: W/System.err(1276): java.io.StreamCorruptedException: Wrong format: ed
10-23 02:20:58.699: W/System.err(1276): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701)
10-23 02:20:58.699: W/System.err(1276): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814)
10-23 02:20:58.699: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
10-23 02:20:58.699: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
10-23 02:20:58.709: W/System.err(1276): at com.wsst.utils.SocketUtil$1.run(SocketUtil.java:77)
10-23 02:20:58.709: W/System.err(1276): at java.lang.Thread.run(Thread.java:856)
10-23 02:20:58.709: W/System.err(1276): java.io.StreamCorruptedException: Wrong format: 0
10-23 02:20:58.709: W/System.err(1276): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701)
10-23 02:20:58.709: W/System.err(1276): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814)
10-23 02:20:58.709: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
10-23 02:20:58.709: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
10-23 02:20:58.709: W/System.err(1276): at com.wsst.utils.SocketUtil$1.run(SocketUtil.java:77)
10-23 02:20:58.709: W/System.err(1276): at java.lang.Thread.run(Thread.java:856)
10-23 02:20:58.719: W/System.err(1276): java.io.StreamCorruptedException: Wrong format: 5
10-23 02:20:58.719: W/System.err(1276): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701)
10-23 02:20:58.719: W/System.err(1276): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814)
10-23 02:20:58.719: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
10-23 02:20:58.719: W/System.err(1276): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
10-23 02:20:58.719: W/System.err(1276): at com.wsst.utils.SocketUtil$1.run(SocketUtil.java:77)
10-23 02:20:58.729: W/System.err(1276): at java.lang.Thread.run(Thread.java:856)
------解决思路----------------------
反序列化问题。
http://hi.baidu.com/chinalwb/item/bffa8745c7cd522011ee1e6b
------解决思路----------------------
出错的相关代码贴一下
------解决思路----------------------
所有的Object传输都是要序列化的,不管集合也好,字符串也好,都是序列化的对象,就会出错。还是贴下你的代码吧。
------解决思路----------------------
Socket传送部分代码
// 接收
public void run() {
try {
while ((mCommend = (Command) mInputStream.readObject()) != null{}
}catch。。。。。
传送跟接收的代码一样一样
如果传送跟接收代码一样,而且传送用的是ObjectOutputStream的话,那么ObjectOutputStream 不能直接追加,因为每次追加都会添加进一个Header。
所以反序列化的时候,才会出现这个异常。
其中一个解决方案是 重写下ObjectOutputStream的writeStreamHeader()函数。