下载文件文件名字乱码

/**
* 根据浏览器的不同,下载文件名的中文的转换
* @param filename
* @return
*/
public String saveFileName(String filename){

HttpServletRequest request=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String agent = request.getHeader("user-agent");
StringTokenizer st = new StringTokenizer(agent,";");
st.nextToken();

String userAgent = st.nextToken();//得到用户的浏览器名

try {
String new_filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");

    // 如果没有UA,则默认使用IE的方式进行编码,因为毕竟IE还是占多数的 
    String rtn = new_filename; 
    if (userAgent != null) 
    { 
        userAgent = userAgent.toLowerCase(); 
        // IE浏览器,只能采用URLEncoder编码 
        if (userAgent.indexOf("msie") != -1) 
        { 
           rtn = URLEncoder.encode(filename, "UTF8");
       } 
       // Opera浏览器只能采用filename* 
       else if (userAgent.indexOf("opera") != -1) 
       { 
           rtn = "*=UTF-8''" + URLEncoder.encode(filename, "UTF8"); 
       } 
       // Safari浏览器,只能采用ISO编码的中文输出 
       else if (userAgent.indexOf("safari") != -1 ) 
       { 
           rtn = new String(filename.getBytes("UTF-8"),"ISO8859-1"); 
       } 
       // Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出 
       else if (userAgent.indexOf("applewebkit") != -1 ) 
       { 
           new_filename = MimeUtility.encodeText(filename, "UTF8", "B"); 
           rtn =  new_filename ; 
       } 
       // FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出 
       else if (userAgent.indexOf("mozilla") != -1) 
       { 
           rtn = new String(filename.getBytes("UTF-8"),"ISO8859-1"); 
       } 
        return rtn;
    } 
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

return null;
}

你可能感兴趣的:(浏览器,chrome,Opera,IE,Safari)