Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to speed up time when using Java Mail to save attachments?

59dd3220-dba0-470a-9af9-8bcc4c9128d8Feb 21 2016 — edited Feb 22 2016

votefavorite

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

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?

I have tried

    props.put("mail.imap.partialfetch", false);

    props.put("mail.imaps.partialfetch", false);

    props.put("mail.imap.fetchsize", "1048576");

but it improved a little.

For more code information https://github.com/cainzhong/java-mail-demo/blob/master/src/main/java/com/java/mail/impl/ReceiveMailImpl.java

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 21 2016
Added on Feb 21 2016
1 comment
1,729 views