1. 程式人生 > >Tomcat解決中文亂碼

Tomcat解決中文亂碼

bsp 字節 for after 查詢參數 utf-8 his blog 屬性

一、中文亂碼原因

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解決中文亂碼