1

There I have code that decodes all attachments in my mail:

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)); }

After encoding and decoding back, I faced some issues, like:

Original PDF: %âãÏÓ

Transformed PDF: %����

Original PDF: H‰d;1 D¯2'°l'Χ¥¡¢@²Ü¿À†XØ&Ò›ÌG~€Épõ·

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

Is there some way not to corrupt content while encoding-decoding?

1
  • 1
    PDF files are binary files. You force the binary data into a string: new String (...). This damages the binary PDF data, depending on the used encoding even beyond repair. Handle binary attachments as binaries instead, e.g. as byte [] or as ByteBuffer. Commented May 10, 2017 at 4:43

1 Answer 1

1

PDF files are binary files; they in particular can contain arbitrary byte sequences.

You force the binary data into a string:

return new String(...); 

Depending on the encoding given or assumed in that transformation, this damages the binary PDF data, probably even beyond repair.

Thus, please handle binary attachments as binaries instead, e.g. as byte[] or as ByteBuffer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.