JAVA文件下載中文名稱亂碼解決方案

    public static String convert(HttpServletRequest request, String fileName) {
        if (fileName == null) {
            throw new IllegalArgumentException("輸入參數是null");
        }
        try{
        	String agent = request.getHeader("USER-AGENT");  
        	if (null != agent && -1 != agent.indexOf("MSIE")) {  
        		return URLEncoder.encode(fileName, "UTF8").replace("+", "%20");  
        	}else if (null != agent && -1 != agent.indexOf("Safari")) {  
        		return new String(fileName.getBytes("utf-8"), "ISO8859-1");  
        	}else if (null != agent && -1 != agent.indexOf("Mozilla")) {  
        		return "=?UTF-8?B?"+(new String(Base64.encodeBase64(fileName.getBytes("UTF-8"))))+"?=";  
        	}  else {  
        		return fileName;  
        	}
        }catch(UnsupportedEncodingException ex){
        	return fileName;
        }
    }

發佈了24 篇原創文章 · 獲贊 23 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章