关于配置Tomcat的URIEncoding

发布时间 2023-04-18 17:08:10作者: 一统天下。

遇到的问题:

     程序需要发送http GET请求到服务器,请求的参数中包含了中文字符。程序中参数为UTF-8格式,且经过了UTF-8 URL编码再发送。使用的tomcat服务器,但服务器端后台程序中取到的参数的中文是乱码。

 

问题原因:

经过分析,应该是Tomcat在解析参数的时候没有使用正确的编码格式(UTF-8)去解码。

查看$TOMCAT_HOME/webapps/tomcat-docs/config/http.html这个说明文档,有如下说明: 
URIEncoding:This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

也就是说,如果没有设置URIEncoding, Tomcat默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,这样的话中文字符肯定就不能被正确解析了。

 

解决办法:

修改Tomcat的pom.xml,如下:

<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>

</build>