2012-07-10 96 views
0

我無法使用IE下載電子郵件中的附件。什麼都沒發生。但我的代碼在Chrome中運行。下面是下載代碼:無法在IE8下載附件(Javamail)

public String downloadattach() { 
    InputStream attStream = null; 
    OutputStream oStream = null; 
    try { 
     String mailid = getRequest().getParameter("mailid"); 
     String filename = getRequest().getParameter("filename"); 
     MailboxServ serv = new MailboxServImpl(); 
     CmMailbox mailbox = serv.getMailbox(mailid); 
     if (mailbox != null && mailbox.getCmessageBody() != null) { 
      Session session = Session.getInstance(System.getProperties(), 
        null); 
      InputStream iStream = mailbox.getCmessageBody() 
        .getBinaryStream(); 
      MimeMessage message = new MimeMessage(session, iStream); 
      MailManage mm = new MailManage(); 
      attStream = mm.getAttach(message, filename); 
      if (attStream != null) { 
       getResponse().setContentType("APPLICATION/OCTET-STREAM");//("APPLICATION/x-msdownload"); 
       getResponse().setContentLength(attStream.available()); 
       getResponse().setHeader("Content-Disposition", 
         "attachment; filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\""); 
       byte[] buff = new byte[2048]; 
       oStream = getResponse().getOutputStream(); 
       int count = 0; 
       while ((count = attStream.read(buff, 0, 1024)) > 0) { 
        oStream.write(buff, 0, count); 
       } 
       oStream.flush(); 
       oStream.close(); 
       attStream.close(); 
       oStream = null; 
      } 
     } else { 
      writeMsg(false, "下載附件出錯!郵件不存在或該郵件不包含指定附件!"); 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
     logger.error(e.toString()); 
     writeMsg(false, "下載附件出錯!錯誤:" + e.getMessage()); 
    } finally { 
     if (attStream != null) { 
      try { 
       attStream.close(); 
      } catch (Exception e2) { 
       // TODO: handle exception 
      } 
     } 
     if (oStream != null) { 
      try { 
       oStream.close(); 
      } catch (Exception e2) { 
       // TODO: handle exception 
      } 
     } 
    } 
    return null; 
} 

在JSP中的鏈接附件:

'<a href="downloadattachMail.action?mailid='+mailboxid+ 
          '&filename='+fname+'" target="_blank">'+fname+'</a>&nbsp;&nbsp;'+fsize 

它只是打開含有鏈接的新窗口中的鏈接(如:本地主機:8081/CMAIL/downloadattachMail.action mailid = 40e4c0b6386e81e801386f0e67ce0018 & filename = java-event中文文件.doc),但什麼都不會發生。而「另存爲」菜單也無法正常工作。有什麼不對的嗎?謝謝!

下載可以在chrome,firefox和IE9下運行。但它不能在IE8中工作(也許不能在IE7和IE6中工作)。

我找到了原因。但它非常奇怪!爲了中國字符解碼,我不得不通過附加改變Tomcat的配置:

URIEncoding="UTF-8" 

如果附件的名稱是英文的,所有的作品好(鉻,IE8,IE9,火狐)。但是如果名字有中文字符,使用ie8時參數變得亂七八糟,同時在chrome,firefox和ie9中一切都很好。 我改變了Tomcat的配置:

URIEncoding="GBK" 

IE8的工作不錯,但瀏覽器,Firefox不能。

我已經找到了解決方案,並分享給大家:

1)。將整個項目設置爲UTF-8,並將jsp設置爲utf-8。 2)。在Js中,使用「encodeURI()」對url進行編碼。 3)。並使用request.getParameter()來獲取它。 再次感謝您的好意!

回答

0

我們要求所有的顯而易見的問題...

是您downloadattach()方法有朝一日能叫什麼名字?

如果是這樣,是否拋出任何異常或檢測到任何其他錯誤?

如果您使用僅ASCII文件名,它有什麼區別?

+0

謝謝你的好意!但我確定我的代碼可以在Chrome,IE9和Firefox下運行。它被調用並執行,即使在IE下也不會拋出異常。它只是不能在IE8下工作(也許IE7和IE6)。 – Joshua 2012-07-11 00:45:20