Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DecodingException when Downloading Attachment. Please advise me how to fix?  RSS feed

Sergey Kargopolov
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting an Error message when downloading attachment from Gmail. Any image file that I download throws com.sun.mail.util.DecodingException:

com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 3 before EOF, the 10 most recent characters were: "rOLsVOnCzb"

Even though an Exception is thrown downloaded file can be opened and viewed. But not good for scaling...

Please advise me what can I do to download file without DecodingException?

Thank you!

InputStream input = bodyPart.getInputStream()
String fileName = bodyPart.getFileName();

FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);

byte buf[]=new byte[1024];
int aByte;
while ((aByte = >0) {

} catch(Exception ex)
} finally{
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!