文件解码后,PDF附件已损坏

时间:2017-05-09 11:30:28

标签: java pdf encoding decoding

我有代码解码我邮件中的所有附件:

for (String key : attachments.keySet()) { String fileContent = attachments.get(key); attachments.put(key, getEncodedPartFromAttachment(fileContent)); }

private String decodeFileContent(String encodedData) { return new String(Base64.getDecoder().decode(encodedData)); }

在编码和解码之后,我遇到了一些问题,例如:

原始PDF:%ÏÏ

转换PDF:%

原始PDF格式:H‰d;1D¯2'°l'¢¢¢@²Ü¿À†XØ&Ò>ÌG~€Épõ·

转换PDF:H d ;1D 2' l'Χ @ ܿ X &қ G~ p

在编码解码时有没有办法不破坏内容?

1 个答案:

答案 0 :(得分:1)

PDF文件是二进制文件;它们尤其可以包含任意字节序列。

您将二进制数据强制为字符串:

return new String(...);

根据转换中给出或假设的编码,这会损坏二进制PDF数据,甚至可能无法修复。

因此,请将二进制附件作为二进制文件处理,例如作为byte[]ByteBuffer