Firefox 火狐 浏览器下载文件文件名中文乱码解决

来源:互联网 发布:淘宝的3c证书编号 编辑:程序博客网 时间:2024/05/18 02:01

chrome IE 浏览器下载正常,火狐下载时文件名中有中文就会乱码

原来 Firefox浏览器自己会对URL进行一次转码。

解决办法:正对不同浏览器区别对待处理


public static String getExplorerType(HttpServletRequest request){String agent = request.getHeader("USER-AGENT");        if(agent != null && agent.toLowerCase().indexOf("firefox") > 0){        return "firefox";         }else if(agent != null && agent.toLowerCase().indexOf("msie") > 0){        return "ie";        }else if(agent != null && agent.toLowerCase().indexOf("chrome") > 0){        return "chrome";          }else if(agent != null && agent.toLowerCase().indexOf("opera") > 0){        return "opera";        }else if(agent != null && agent.toLowerCase().indexOf("safari") > 0){        return "safari";        }        return "others";}


public ExportExcel write(HttpServletRequest request,HttpServletResponse response, String fileName) throws IOException{response.reset();        response.setContentType("application/octet-stream; charset=utf-8");        if("firefox".equals(ExplorerUtil.getExplorerType(request))){        //火狐浏览器自己会对URL进行一次URL转码所以区别处理        response.setHeader("Content-Disposition", "attachment; filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));        }else{        response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(fileName));        }        write(response.getOutputStream());return this;}


      


阅读全文
0 0
原创粉丝点击