Java web upload/download garbled problem solution: web upload/download

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.