开发者

javamail: why is text/plain attachment matter returned by getContent, and not from InputStream?

In trying to download mail with attachments from gmail, my test mail includes a text file as attachment.

The attachment part returns Text attachment content-type, and even the filename correctly. But the loop condition over attachment Inp开发者_JAVA技巧utStream is never non-zero.

After a bit of trial and error it turned out that the content for text/plain is available using the getContent method for the part (in the case below introducing the call

    att_mbp.getContent() 

returned the content in the attached text file)

if (BodyPart.ATTACHMENT.equalsIgnoreCase(att_mbp.getDisposition())) {

                att_mbp.getContentType();

                // process each attachment
                // read the filename
                file = att_mbp.getFileName();

                InputStream stream = att_mbp.getInputStream();

                BufferedInputStream br = new BufferedInputStream(stream);
                BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file));

                while (br.available() > 0) {
                   // this loop is never executed for text/plain
                    bout.write(br.read());
                }
                bout.flush();
                bout.close();

}

My question is - Why is the text/plain attachment body only available from getContent(), and not from the attached InputStream instance too?


Ok. I finally figured it out.

The call to available () always returns 0. The code worked when I modified it as follows

int dataByte;

while( ( dataByte = br.read() ) > 0 ){
  bout.write( dataByte );
}

According to javadoc, descendants of InputStream should override available. It looks like this is not the case here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜