admin管理员组

文章数量:1530938

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;
	}


      


本文标签: 火狐乱码文件名中文浏览器