方法一:web
修改server.xml: URIEncoding="utf-8"spring
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />tomcat
方法二:app
String parameter = request.getParameter("auth_id");url
String string = new String(parameter.getBytes("iso-8859-1"), "utf-8");server
(1)tomcat7,URL中參數的默認解碼是ISO-8859-1,而tomcat8的默認解碼爲utf-8。xml
(2)在過濾器中使用 request.setCharacterEncoding() 只能用來處理 POST 請求,對 GET 則無效。utf-8
(3)tomcat亂碼的終極解決辦法:get
tomcat7:設置URIEncoding + Post的過濾器string
tomcat8:只要設置一個Post過濾器就能夠了,tomcat9跟tomcat8是同樣的。
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>