如何在使用Java Mail保存附件时加快时间? [英] How to speed up time when using Java Mail to save attachments?
问题描述
我将 Message msg 分成 Multipart multi1 =(Multipart)msg.getContent()。
邮件附件在一个BodyPart中, Part part = multi1.getBodyPart(i);
然后我想保存附件。
I separate Message msg into Multipart multi1 = (Multipart) msg.getContent(). And a mail attachment is in one BodyPart, Part part = multi1.getBodyPart(i); Then I want to save the attachment.
private void saveFile(String fileName, InputStream in) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
in = new BufferedInputStream(in);
byte[] buf = new byte[BUFFSIZE];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
LOG.error(e.toString());
} finally {
// close streams
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
但是花费太多时间阅读IO Stream。例如,2.7M文件需要大约160秒才能保存在磁盘上。我已经尝试过Channel和其他一些IO Stream,但没有任何改变。使用Java Mail保存附件的任何解决方案?
But it cost too much time on reading IO Stream. For example,a 2.7M file needs almost 160 seconds to save on the disk. I have already tried Channel and some other IO Stream, but nothing changed. Any solution for saving attachment using Java Mail?
推荐答案
实际上,mail.imaps.partialfetch生效并加速很多。我以前的代码有一个错误。
Actually, mail.imaps.partialfetch takes effect and speeds up a lot. There is a mistake for my previous code.
props.put("mail.imap.partialfetch","false");
props.put("mail.imap.fetchsize", "1048576");
props.put("mail.imaps.partialfetch", "false");
props.put("mail.imaps.fetchsize", "1048576");
而不是
props.put("mail.imap.partialfetch",false);
props.put("mail.imap.fetchsize", "1048576");
props.put("mail.imaps.partialfetch", false);
props.put("mail.imaps.fetchsize", "1048576");
在false上加上引号很重要。如果没有,参数将不起作用。
It is important to put a quotation mark on "false". If not, the parameters will not take effects.
无论如何,多亏Bill Billnon。
Anyway, thanks to Bill Shannon.
这篇关于如何在使用Java Mail保存附件时加快时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!