【问题标题】:javamail: why is text/plain attachment matter returned by getContent, and not from InputStream?javamail:为什么getContent返回文本/纯附件,而不是InputStream?
【发布时间】:2011-10-14 04:18:31
【问题描述】:

在尝试从 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 实例获得?

【问题讨论】:

    标签: jakarta-mail attachment inputstream plaintext


    【解决方案1】:

    好的。我终于弄明白了。

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

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

    根据 javadoc,InputStream 的后代应该覆盖可用的。看来这里不是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多