2012-07-10 59 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