以下是我开发邮件发送和文件上传遇到的问题,总结如下:
?
1.
问题描述:下载文件时,出现空格或者中文乱码?
解决办法:
?
下载文件时:
response.reset(); // 非常重要 very important
??? ??? response.setContentType("application/msexcel;charset=UTF-8");
??? ??? String fileName =f.getName();
??? ??? String userAgent = request.getHeader("User-Agent");?
??? ??? if(userAgent != null && userAgent.indexOf("MSIE") == -1) {??
??? ??? ??? // FF???
??? ??? ??? String enableFileName = "=?UTF-8?B?" + (new
String(org.apache.commons.codec.binary.Base64.encodeBase64(fileName.getBytes("UTF-8"))))
+ "?=";??
??? ??? ??? response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
??? ??? }else{
??? ??? ??? // IE???
??? ??? ??? String enableFileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");???
??? ??? ??? response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);??
2.
问题描述:发送邮件时,接收端邮件标题乱码,附件乱码?
解决办法:
2.1:接收端邮件标题乱码,附件乱码的原因,我总结了两点:
A:中文乱码
B:空格乱码
具体的解决办法:
??? ??? ??? sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
??? ??? ??? fileName = "=?GBK?B?" + enc.encode(fileName.getBytes()) + "?=";
?
?
以上描述仅作个人总结自用!