java.lang.Integer 的 Java 反序列化 - 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/851491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Deserialization of java.lang.Integer - Exception
提问by Michael Borgwardt
Recieved the following exception when deserializing a HashMap<String, Integer>:
反序列化 a 时收到以下异常HashMap<String, Integer>:
java.io.InvalidClassException: java.lang.Integer; local class incompatible: stream classdesc serialVersionUID = 1360826667802527544, local class serialVersionUID = 1360826667806852920
Serialized and deserialized on the same machine, with the same JRE. JDK 1.6.0_12
在同一台机器上序列化和反序列化,使用相同的 JRE。JDK 1.6.0_12
回答by Laurence Gonsalves
From looking at the JDK source, 1360826667806852920 is the correct serialVersionUIDfor Integer. I wasn't able to find any classes in the JDK with the serialVersionUID1360826667802527544.
从看JDK源,1360826667806852920是正确serialVersionUID的Integer。我无法在带有serialVersionUID1360826667802527544的 JDK 中找到任何类。
Interestingly, searching for 1360826667802527544 on Google turned up a few other people with this problem, notably this threadon Sun's forums. The problem there was that the person was storing bytes in a String, and the serialized data was getting mangled. Since you're getting the same serialVersionUIDit seems very likely that you're running into a similar problem.
有趣的是,在 Google 上搜索 1360826667802527544 发现了其他一些有此问题的人,尤其是Sun 论坛上的这个帖子。问题是这个人将字节存储在一个字符串中,并且序列化的数据被破坏了。由于您得到了相同的结果,serialVersionUID因此您很有可能遇到了类似的问题。
Never store bytes in a String. Use a byte array or a class designed to hold bytes, not chars.
永远不要将字节存储在String. 使用字节数组或设计用于保存字节而不是字符的类。
回答by Satyen Shimpi
I faced the same issue and it is because when we are trying to store Integer object to String, the character encoding is getting messed up and while deserialization the serialVersionUID read is wrong. That's the root cause of this error. To avoid this error use Base64encodingbefore storing it to String. see this answerand the problem resolved for me.
我遇到了同样的问题,这是因为当我们尝试将 Integer 对象存储到 String 时,字符编码变得混乱并且在反序列化时 serialVersionUID 读取是错误的。这就是这个错误的根本原因。为避免此错误,请在将其存储到 String 之前使用Base64编码。 看到这个答案并为我解决了问题。
/** Read the object from Base64 string. */
private static Object fromString( String str ) throws IOException, ClassNotFoundException {
byte [] data = Base64.getDecoder().decode(str);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
}
/** Write the object to a Base64 string. */
private static String toString( Serializable o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
回答by pgras
check the source code for Integer, here is what I have for Integer in several verions of java:
检查 Integer 的源代码,这是我在几个版本的 java 中为 Integer 所拥有的:
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1360826667806852920L;
So I'd say the problem comes from a class of yours that you changed between serialization and deserialization and that has no specific serialVersionUID...
所以我会说问题来自你的一类,你在序列化和反序列化之间改变并且没有特定的 serialVersionUID ...
Maybe you should look at this, same problem description and it looks like wrong serialization / deserialization code....
也许你应该看看这个,同样的问题描述,它看起来像是错误的序列化/反序列化代码......
回答by Michael Borgwardt
That shouldn't happen. Note that the IDs differ only in the last few digits; the second one ist the one I see in my JDK sources.
那不应该发生。请注意,ID 仅在最后几位数字上有所不同;第二个是我在 JDK 源代码中看到的那个。
My guess is that the serialized stream got corrupted somehow.
我的猜测是序列化的流以某种方式损坏了。

