我无法使用IE在电子邮件中下载附件。没啥事儿。但我的代码适用于Chrome。这是下载代码:
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> '+fsize
它刚刚打开了一个链接链接的新窗口(例如:localhost:8081 / CMAIL / downloadattachMail.action?mailid = 40e4c0b6386e81e801386f0e67ce0018&amp; filename = java-event中文文件.doc),但什么都不会发生。而“另存为”菜单也无法正常工作。有什么不对的吗?谢谢!
我找到了原因。但这非常奇怪!为了解码汉字,我通过添加:
改变了tomcat的配置 URIEncoding="UTF-8"
如果附件的名称是英文,则一切正常(chrome,ie8,ie9,firefox)。但是如果这个名字有中文字符,那么当使用ie8时参数变得一团糟,同时在chrome,firefox和ie9中一切都很好。 我改变了tomcat的配置:
URIEncoding="GBK"
我找到了解决方案并将其分享给所有人:
1)。将整个项目设置为UTF-8,将jsp设置为utf-8。 2)。在Js中,使用“encodeURI()”对url进行编码。 3)。并使用request.getParameter()来获取它。 再次感谢您的好意!
答案 0 :(得分:0)
让我们问一下所有明显的问题......
你的downloadattach()方法是否被调用?
如果是,是否会抛出任何异常或检测到任何其他错误?
如果您使用纯ASCII文件名,它会有什么不同吗?