我尝试从用于存储Spring会话的Redis服务器将String反序列化为Java Object,我想在Spring框架之外反序列化它。我认为Spring Redis序列化程序可能使用默认字符集UTF-8将Java Object序列化为String。
Redis中的字符串:
\ XAC \ XED \ X00 \ x05sr \ X00 \ x0Ejava.lang.Long; \ x8B \ XE4 \ X90 \ XCC \ X8F#\ XDF \ X02 \ X00 \ x01J \ X00 \ x05valuexr \ X00 \ x10java.lang。数\ 86 \ XAC \ X95 \ X1D \ X0B \ X94 \ xE0 \ x8B \ X02 \ X00 \ x00xp \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ XD7
我使用的方法:
private static void scanKeys(Jedis jedis, String prefix, String hashKey)
throws IOException, ClassNotFoundException {
Set<String> keys = jedis.keys(prefix + "*");
for (String key : keys) {
if (!key.contains("expir")) {
Map<String, String> sessionMap = jedis.hgetAll(key);
for (Entry<String, String> entry : sessionMap.entrySet()) {
if (entry.getValue() != null && entry.getKey().equals(hashKey)) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
byte[] output = entry.getValue().getBytes(charset);
System.out.println(Arrays.toString(output));
try {
Long id = (Long) deserialize(output);
System.out.println(id);
} catch (EOFException e) {
break;
}
}
}
}
}
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
}
错误消息:
java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:354)
at practice.redis.jedis.App.deserialize(App.java:191)
at practice.redis.jedis.App.scanKeys(App.java:168)
at practice.redis.jedis.App.main(App.java:77)
我知道byte []和字符串与UTF-8之间的转换可能就是问题所在,但我仍然想问一下是否有人知道如何解决这个问题而不修改序列化部分。
答案 0 :(得分:0)
我建议你自己解析一下这个字符串。它看起来像是一个简单的转义和非转义字节序列。
String value = "\\xAC\\xED\\x00\\x05sr\\x00\\x0Ejava.lang.Long;\\x8B\\xE4\\x90\\xCC\\x8F#\\xDF\\x02\\x00\\x01J\\x00\\x05valuexr\\x00\\x10java.lang.Number\\x86\\xAC\\x95\\x1D\\x0B\\x94\\xE0\\x8B\\x02\\x00\\x00xp\\x00\\x00\\x00\\x00\\x00\\x00\\xD7";
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
int i = 0;
while (i < value.length()) {
char ch = value.charAt(i);
if (ch == '\\') {
if (value.charAt(i + 1) != 'x') {
throw new UnsupportedOperationException();
}
String hex = value.substring(i + 2, i + 4);
byteArrayOutputStream.write(Integer.parseInt(hex, 16) & 0xFF);
i += 4;
} else {
byteArrayOutputStream.write(ch);
i++;
}
}
byte[] output = byteArrayOutputStream.toByteArray();
Long id = (Long) deserialize(output);
System.out.println(id);
}