开发者

Decode Base64 content from a MimeMultipart

I have a Base64 file into one part of my MimeMessage. I put it like this

DataSource source = new FileDataSource(new File("base64Test.bin"));
bodyPart.setDataHandler(new DataHandler(source));

Then, I want to decode it using the method BASE64DecoderStream.decode

String base64 = (String)bodyPart.getContent();
byte [] base64Decoder = BASE64DecoderStream.decode(base64.ge开发者_C百科tBytes());

The problem with that is that I have always an ArrayOutOfboundexception when I use this method.

Any advice in order to solve this problem?


As far as I know, a Base64DecoderStream takes an InputStream, not a byte array. At a minimum, you need to change the decoding like so:

ByteArrayInputStream contentStream = new ByteArrayInputStream(base64Content.getBytes());
BASE64DecoderStream decodeStream = new BASE64DecoderStream(contentStream);
int decodedByte
while ((decodedByte = read()) != -1) {
   // handled decoded bytes
}

Perhaps autoboxing is trying to help you out, and some "intermediate" array creation is fouling the works. Or, maybe your message is just large enough that an array can't be created big enough. Or maybe you have two threads calling the static offering which is messing up internal shared buffers.


I read the BASE64DecoderStream like this:

DataHandler handler = bodyPart.getDataHandler();
System.out.println("attachment name : " + handler.getName());
                    
FileOutputStream fos = new FileOutputStream(new File(handler.getName()));
handler.writeTo(fos);

this OutPutStream is having data which has been written to file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜