【问题标题】:Java Mail Slow downloading attachment Office 365Java Mail 慢速下载附件 Office 365
【发布时间】:2017-03-14 05:19:59
【问题描述】:

我正在阅读 Office 365 Mail 帐户的收件箱并下载其附件(txt 文件)。它适用于小于 2 MB 的小文件,但是当我尝试使用 20 MB 文件时,它真的很慢。我在两台不同的机器(Linux和Windows)上测试代码,速度是一样的,真​​的很慢。

String user = "mail@mail.com";
String password = "mypassword";
try{

    boolean foundSites = false;

    // Get a Properties object
    Properties properties = new Properties();

    properties.put("mail.imaps.host", host);
    properties.put("mail.imaps.port", port);
    properties.put("mail.imaps.starttls.enable", SSL);
    //properties.put("mail.imap.fetchsize", "1000000");
    Session emailSession = Session.getDefaultInstance(properties);

    //create the POP3 store object and connect with the pop server
    Store store = emailSession.getStore("imaps");
    store.connect(host, user, password);

    //create the folder object and open it
    Folder emailFolder = store.getFolder("INBOX");
    emailFolder.open(Folder.READ_ONLY);

    // retrieve the messages from the folder in an array and print it
    Message[] messages = emailFolder.getMessages();
    System.out.println("messages.length---" + messages.length);

    LocalDateTime now = LocalDateTime.now();
    int year = now.getYear();
    int month = now.getMonthValue();
    int day = now.getDayOfMonth();

    for (int i = messages.length - 1; i >= 0; i--) {
    Message message = messages[i];


    Calendar cal = Calendar.getInstance();
    cal.setTime(message.getReceivedDate());
    int yearMessage = cal.get(Calendar.YEAR);
    int monthMessage = cal.get(Calendar.MONTH) + 1;
    int dayMessage = cal.get(Calendar.DAY_OF_MONTH);



    if(year == yearMessage && month == monthMessage && day == dayMessage)
    {
        //The real code is doing this with 20 Subjects (20 emails)
        if(message.getSubject().equalsIgnoreCase("Integration - Site Info") && foundSites != true)
        {
            System.out.println("found Integration - Site Info");

            Multipart multipart = (Multipart) message.getContent();
            List<File> attachments = new ArrayList<>();
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
                {
                    InputStream is = bodyPart.getInputStream();
                    File f = new File("./attachments/"
                            + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv");
                    FileOutputStream fos = new FileOutputStream(f);
                    byte[] buf = new byte[4096];
                    int bytesRead;
                    while((bytesRead = is.read(buf))!=-1) {
                        fos.write(buf, 0, bytesRead);
                    }
                    fos.close();
                    attachments.add(f);
                    foundSites = true;
                    break;
                }


            }
        }
    }

    if(foundSites)
    {
        break;
    }
} catch (Exception e) {
    System.out.println(e);  
}

我可以创建线程,但还有其他选择吗?

只是一个旁注: 我用python尝试了代码,速度明显提高。

======================================= 更新 1:

我对 saveFile 方法进行了更改,代码简化了很多,但仍然是每秒 10 KB 的下载速度。

MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(j);
if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
{
    bodyPart.saveFile("./attachments/"
        + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv");

我也使用分析器,结果是:

【问题讨论】:

  • 有多慢才是真正的慢?多快才够快?
  • 你分析过代码吗?用 jvisualvm 运行它,告诉我们哪个部分慢。在我的脑海中,您可能将缓冲添加到FileOutputStream

标签: java jakarta-mail


【解决方案1】:

修复这些common mistakes

使用MimeBodyPart.saveFile 保存附件来简化您的代码。

由于您使用的是“imap”协议而不是“imap”协议,请将mail.imaps.fetchsize 属性设置为足够大的值,以便在不使用超出您想要使用的内存的情况下获得您想要的性能。或者,如果您不在乎使用了多少内存,并且确信您将永远有足够的内存,请将 mail.imaps.partialfetch 设置为 false。

【讨论】:

  • 我做了 saveFile 方法,它确实简化了代码,但下载速度仍然在每秒 10 KB。
  • 您为什么不尝试我建议的其他更改?
  • 我将mail.imaps.partialfetch 属性设置为false,将mail.imaps.fetchsize 设置为2000000 现在速度真的提高了!谢谢!您对如何优化计算此值有一些建议吗?
  • 实验。这是速度和内存之间的权衡。
猜你喜欢
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
相关资源
最近更新 更多