Tomcat解决中文乱码

时间:2023-01-10 18:10:35

一、中文乱码原因

Tomcat默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,中文字符不能被正确解析了。

 

二、配置编码

在tomcat的conf/server.xml下的connetor属性中增加URIEncoding或者useBodyEncodingForURI属性

(1)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.

译文:这指定了用于解码URI字节的字符编码。如果没有指定,将使用ISO-8859-1。

 如:

<Server port="8005" shutdown="SHUTDOWN">
<!-- 其他配置 -->
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />
<!-- 其他配置 -->
</Service>
</Server>

 

(2)useBodyEncodingForURI

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding.

译文:这指定编码指定contentType应使用URI查询参数,而不是使用URIEncoding。

如:

<Server port="8005" shutdown="SHUTDOWN">
<!-- 其他配置 -->
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" />
<!-- 其他配置 -->
</Service>
</Server>