I separate Message msg into Multipart multi1 =(Multipart)msg.getContent()。 並且郵件附件位於一個BodyPart中,零件= multi1.getBodyPart(i); 然後我想保存附件。如何加快使用Java Mail保存附件的時間?
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流時花費太多時間。例如,一個2.7M文件需要將近160秒才能保存在磁盤上。我已經嘗試過Channel和其他IO Stream,但沒有任何改變。任何使用Java Mail保存附件的解決方案?
它似乎有所改善。但只是一點。 使用props.put(「mail.imap.partialfetch」,false);props.put(「mail.imaps.partialfetch」,false); props.put(「mail.imap.fetchsize」,「1048576」); – Cain
確保您使用mail.imap。*或mail.imaps。*屬性,具體取決於您在getStore調用中使用的協議名稱。如果速度不夠快,那麼您會遇到網絡或服務器速度限制。 –
你好,你的'fetchsize'和'partialfetch'答案中的URL現在已經過時了。你可以請更新嗎? – kevinarpe