Jackson和文件编码的奇怪错误

时间:2012-09-24 23:03:50

标签: java json amazon-s3 jackson

我正在从S3中提取文件,当我使用内容流作为裸InputStream调用对象映射器时,解码失败并出现UTF-8异常,但是当我使用包装InputStream的BufferedReader时,它工作正常

如果我将文件读入本地文件,然后将其作为FileInputStream打开,也可以正常工作。我很困惑。我希望有人在我面前遇到过这个问题,或者对Jackson的编码方面的裸输入流与BufferedReader的工作方式有所了解。

这失败

S3Object s3o = s3Client.getObject("my-bucket","my-key");
Object t = om.readValue(s3o.getObjectContent(), Object.class);

这有效

S3Object s3o = s3Client.getObject("my-bucket","my-key");
Object t = om.readValue(new BufferedReader(new InputStreamReader(s3o.getObjectContent())), Object.class);

错误:

org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x5c
at [Source: org.apache.http.conn.EofSensorInputStream@6460029d; line: 1, column: 31611]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
    at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2830)
    at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2837)
    at org.codehaus.jackson.impl.Utf8StreamParser._decodeUtf8_2(Utf8StreamParser.java:2625)
    at org.codehaus.jackson.impl.Utf8StreamParser._finishString2(Utf8StreamParser.java:1952)
    at org.codehaus.jackson.impl.Utf8StreamParser._finishString(Utf8StreamParser.java:1905)
    at org.codehaus.jackson.impl.Utf8StreamParser.getText(Utf8StreamParser.java:276)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:59)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.mapObject(UntypedObjectDeserializer.java:218)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:47)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.mapArray(UntypedObjectDeserializer.java:165)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:51)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.mapObject(UntypedObjectDeserializer.java:218)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:47)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.mapObject(UntypedObjectDeserializer.java:196)
    at org.codehaus.jackson.map.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:47)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1909)

1 个答案:

答案 0 :(得分:2)

您的内容不是UTF-8,而是对ISO-8859-1(Latin-1)等JSON无效的内容。您对BufferedReader的使用也有点错误 - 您应该指定编码,否则使用平台默认编码(可能是任何东西) - 但它可能会从该编码转换以避免错误。 尽管如此,听起来内容不是有效的JSON,无论谁生成它都应该修复它以使用支持的编码之一(UTF-8或UTF-16)。