• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

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

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;
try
{
while ((aByte = bis.read(buf)) >0) {
bos.write(buf,0,aByte);
}

} catch(Exception ex)
{
ex.printStackTrace();
} finally{
bos.flush();
bos.close();
bis.close();
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic