1

I need to send AWS s3 file as email attachment in Base64 encoded. So reading and writing file content in streams, encoding each byte array in Base64.

The attachment is fine with text files but not getting decoded properly with pdf files.

Below is the sample code to write into outputStream. Reading file content in 3 bytes for proper Base64 encoding and decoding

byte[] bbuf = new byte[3];
while ((input = in.read(bbuf)) != -1)
        {
            // Url ByteArrayOutputstream
            wr.write(Base64Encoder.encode(bbuf).getBytes());
            wr.flush();

            // Also writing to File locally to know received content
            fs.write(bbuf, 0, input);
            fs.flush();

        }

I tried this solution which worked fine while writing to File but not with Base64 encoded email attachment.

I also tried encoding only 0-input byte array and writing those encoded bytes, which also failed.

Please help me,

Thanks

Community
  • 1
  • 1
Naresh M
  • 113
  • 11
  • More details on `Base64Encoder` pls and on `wr` variable. – Serhiy Oct 10 '14 at 13:12
  • Hi Serhiy, I used Apache codec Base64, org.apache.commons.codec.binary.Base64.encodeBase64(bbuf) instead which results in no difference. The wr is URLConnection conn = url.openConnection(); ByteArrayOutputStream wr = (ByteArrayOutputStream)conn.getOutputStream(); – Naresh M Oct 10 '14 at 19:02
  • I will post you solution I have in one of mine applications. I remember that I had some problem related with Image encoding and it had actually had to do with byte buffers and Base64 encoding (can't remember very well problem itself) `Base64.encodeBase64String(IOUtils.toByteArray(item.getInputStream()));` I suggest you to use some online encoding tool and check whenever the encodedstring is the same. – Serhiy Oct 13 '14 at 06:37

0 Answers0