0

在尝试从 gmail 下载带有附件的邮件时,我的测试邮件包含一个文本文件作为附件。

附件部分正确返回文本附件内容类型,甚至文件名。但是附件 InputStream 上的循环条件永远不会非零。

经过一番反复试验,结果证明 text/plain 的内容可以使用部件的 getContent 方法获得(在下面介绍调用的情况下

    att_mbp.getContent() 

返回附加文本文件中的内容)

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();

}

我的问题是 - 为什么文本/纯文本附件正文只能从 getContent() 获得,而不能从附加的 InputStream 实例获得?

4

1 回答 1

0

行。我终于弄明白了。

对available()的调用总是返回0。当我修改它时代码工作如下

int dataByte;

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

根据 javadoc, InputStream 的后代应该覆盖可用的。看起来情况并非如此。

于 2011-07-28T17:04:05.927 回答