The Chinese garbled problem of URL parameter in Java

Source: Internet
Author: User

**同学们在url传参的时候 是否应为传了中文导致各种乱码问题呢?URL传参中文对于程序员来说,是个很头疼的问题吧。虽然在尽量避免使用中文参数,但总有避免不了的时候。现就自己的经验,总结如下:**

1. Transcode The string: newstring ("xxxxx". GetBytes ("Iso-8859-1"), "Utf-8")
This transcoding method has great drawbacks because it uses the specified character set to encode this string into a byte sequence, stores the result in a new byte array, and then decodes the resulting byte array by using the specified character encoding to construct a new string string. This situation is likely to encounter the situation is that a Chinese character can not be completely decoded. In this way, the front can be displayed normally, but the last word may be garbled.
So it is not recommended to use this method.

2. Transfer the code before the parameter, receive the parameters and then transcode back.
There are two types of this approach:
The first type:
Before the reference: Use Java.net.URLEncoder.encode ("xxxx", "Utf-8") to convert Chinese into 16 characters.
After receiving the parameters: Use Java.net.URLDncoder.decode ("xxxx", "Utf-8") to convert the 16 characters into Chinese.
It is important to note that when you use encode transcoding, special characters appear, and you need to replace the special characters with the corresponding 16 binary. Because special characters are passed as arguments in the URL path, they are garbled as well.

The second type:
Before reference: encodeURI ("xxxx").
After receiving the parameters: Use Java.net.URLDncoder.decode ("xxxx", "Utf-8") to convert the 16 characters into Chinese.
In this way, it is important to note that after using encodeURI transcoding, special characters will appear, when special characters are also transcoded, so the use of two times encodeURI, namely:
encodeURI (encodeURI ("xxxx")).

These two transcoding methods are very useful, so it is recommended that you use.

    1. To modify the Tomcat configuration file:

In the Server.xml file in the Conf folder under the Tomcat installation directory, add the uriencoding= "UTF8" to the location where the access port is configured.<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" URIEncoding="GBK">

The Chinese garbled problem of URL parameter in Java

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.