Java web upload/download garbled problem solution: web upload/download
Chinese characters of file downloading are garbled, because the http request url and header must only pass the ascii code, but not other characters. transcoding is required. Different browsers have different processing methods on the right.
Solution 1:
/*** Garbled solution * @ 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 ");}}
Solution 2:
This method will not be garbled during the download of the new versions of firefox, ie, and chrome in the windows Chinese system. I do not know if the default character set of other systems is non-gbk.
New String (fileName. getBytes ("gbk"), "ISO8859-1 ")
References:
Http://www.ruanyifeng.com/blog/2010/02/url_encoding.html