JavaWeb中遇到的字符编码问题

一、常见的编码方式

1、UTF-8

2、ISO-8859-1

二、Tomcat的编码问题

Tomcat8和7的编码方式

Tomcat7对URI默认编码是ISO-8859-1
Tomcat8对URI默认编码是UTF-8

官网解释:
https://tomcat.apache.org/tomcat-7.0-doc/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.
https://tomcat.apache.org/tomcat-8.0-doc/config/http.html
URIEncoding :This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

在Tomcat包内也可以查找到:webapps→docs→config→http.html

URIEncoding 和useBodyEncodingForURI

以Tomcat8为例:

(1)、URIEncoding

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

(2)、useBodyEncodingForURI

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.
Notes: 1) This setting is applied only to the query string of a request. Unlike URIEncoding it does not affect the path portion of a request URI. 2) If request character encoding is not known (is not provided by a browser and is not set by SetCharacterEncodingFilter or a similar filter using Request.setCharacterEncoding method), the default encoding is always "ISO-8859-1". The URIEncoding setting has no effect on this default.

可以理解为:
设置URIEncoding参数可以是对所有GET方式的请求的数据进行统一的重新编码,默认编码为 UTF-8;
设置useBodyEncodingForURI为true时,请求查询参数的编码和contentType的设置一致

三、解决方案

1、URIEncoding

    <Connector connectionTimeout="20000" port="9080" protocol="HTTP/1.1"  
    redirectPort="443" URIEncoding="UTF-8" />

URIEncoding只对URL中的参数进行编码

2、useBodyEncodingForURI="true"

设置useBodyEncodingForURI=true时,就会将请求参数和请求体中的参数根据request.setCharacterEncoding或者contentType中的字符集编码。

3、request.setCharacterEncoding("UTF-8")

servlet等Java后台程序中使用request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");这个方法对于请求Body中的字符编码才有作用,也就是基本只对POST产生作用,终究的原因是GET是HTTP服务器处理,而POST是WEB容器处理

4、get请求的处理

首先明确乱码原因:Http请求传输时将url以ISO-8859-1编码,服务器收到字节流后默认会以ISO-8859-1编码来解码成字符流;
特别是对于get请求,以上方法不适用的情况下,可以通过字符串和字节流转换时使用正确的编码获取中文参数
String str = new String(request.getParameter("参数名").getBytes("iso-8859-1"), "utf-8");

5、encodingFilter

<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>

6、tomcat7-maven-plugin插件

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <port>9080</port>
        <server>tomcat7</server>
        <uriEncoding>UTF-8</uriEncoding>    
    </configuration>
</plugin>

参考资料:

1、Tomcat7项目迁移到Tomcat8中文乱码问题
2、Java web中常见编码乱码问题(一)

https://blog.csdn.net/liu865033503/article/details/81094575

posted @ 2020-06-04 19:17  Dreamice  阅读(368)  评论(0编辑  收藏  举报