Java: IE & Firefox下载文件中文乱码的兼容代码

来源:互联网 发布:软件测试工具下载 编辑:程序博客网 时间:2024/05/16 15:44
需要使用两种不同编码方式来处理,在Firefox 3.6,IE 8以及google下测试通过:

String agent = request.getHeader("User-Agent");
boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);

if (isMSIE) {
    fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
0 0