某些浏览器下载文件乱码问题

1,我的问题,ie11下载文件时出现了乱码,

2,问题定位:

/*if (userAgent.indexOf("msie") != -1) {
fileName= new String(name.getBytes("gb2312"), "ISO8859-1" );
}else{
fileName = new String(name.getBytes("UTF-8"),
"ISO8859-1");
}*/
//处理方式不正确。
3,断点后发现了,各种浏览器userAgent类型如下:

IE11:mozilla/5.0 (windows nt 6.3; trident/7.0; rv:11.0) like gecko

IE10:mozilla/5.0 (compatible; msie 10.0; windows nt 6.2; trident/6.0)

IE9:mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0)

IE8:mozilla/4.0 (compatible; msie 8.0; windows nt 6.1; trident/4.0)

Firefox:Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0

Chrome:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

Opera:mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/60.0.3112.90 safari/537.36 opr/47.0.2631.80

4,根据以上类型更正代码如下:

if (StringUtils.contains(userAgent, "msie") || StringUtils.contains(userAgent, "trident")) {//IE浏览器
fileName = URLEncoder.encode(name,"UTF8");
} else if (StringUtils.contains(userAgent, "mozilla")) {//google,FireFox
fileName = new String(name.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(name, "UTF8");//其他浏览器

}
5,注意:StringUtils 判断是否包含,未判定大小写,所以一定要注意大小写问题。

 

 

上一篇:js同过url下载文件,调用另存为弹框


下一篇:Android Framework实战视频--BootAnimation的启动源码分析(Android8.1)