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

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 10.0; win64; x64; rv:66.0) gecko/20100101 firefox/66.0

Chrome:mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/67.0.3396.99 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, "safari")){
fileName = URLEncoder.encode(name, "UTF8");//Opera浏览器 ,google
}else if (StringUtils.contains(userAgent, "chrome")||StringUtils.contains(userAgent, "firefox")) {//google,FireFox
fileName = new String(name.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(name, "UTF8");//其他浏览器

}
//因为Google 兼容了 chroome和safafi 所以,google浏览器可以适应两种编码方式。

 

 

posted @ 2019-04-22 16:44  莫追莫缘求  阅读(940)  评论(0编辑  收藏  举报