一、中文乱码原因
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>
本文介绍了Tomcat中出现中文乱码的原因,并提供了两种解决方法:通过设置URIEncoding属性来指定URI解码使用的字符编码,或者启用useBodyEncodingForURI属性来使用contentType指定的编码。
360

被折叠的 条评论
为什么被折叠?



