文件下載中文亂碼,因為http請求url和header要求只能通過ascii碼,對于其他字符則不行,需要轉(zhuǎn)碼。而不同瀏覽器的處理方式右不一樣。
解決方法一:
/** * 亂碼解決 * @throws UnsupportedEncodingException * */PRivate static String toUtf8BytesString(String fileName, HttpServletRequest req) throws UnsupportedEncodingException {//return new String(fileName.getBytes("GBK"), "ISO8859-1");if(req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){return URLEncoder.encode(fileName, "UTF-8");}else{return new String(fileName.getBytes("UTF-8"), "ISO8859-1");}}
解決方法二:
這種方法在 windows 中文系統(tǒng)新版Firefox、ie、Chrome下下載不會亂碼,不知道其他系統(tǒng)默認(rèn)字符集非gbk的會不會有問題
new String(fileName.getBytes("gbk"), "ISO8859-1")參考資料:
http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
新聞熱點(diǎn)
疑難解答