servlet下载文件名称乱码
文件名为fileName="人员信息表"+sessionid+".xls";:
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition","attachment;filename="+fileName);
这样肯定会出现乱码
?
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
如果加上这步处理,在firefox系浏览上正常
看下抓包显示:
Content-disposition: attachment;filename=%E4%BA%BA%E5%91%98%E4%BF%A1%E6%81%AF%E8%A1%A8A62A652E0F8A27B5B4B4F09FCC11B4C7.xlsContent-Type: application/vnd.ms-excel;charset=UTF-8
很明显可以看出是中文的UTF-8经iso传输
?
对于IE刚不同:
?fileName = URLEncoder.encode(fileName, "UTF-8");
Content-disposition: attachment;filename=...............DE043979618C4EB99C305E284B7DE90A.xlsContent-Type: application/vnd.ms-excel;charset=UTF-8
?...在抓到的数据中显示为实体如:\344\272\272等等
?
所以要根据浏览器进行判断
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
else?
? ? if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
? ? fileName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
?
?