**同学们在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.
- 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