• 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

  • 相关阅读:
    GIF文件转换为头文件工具
    深夜杂想
    swift项目第十天:网络请求工具类的封装
    swift项目第九天:正则表达式的学习
    swift项目第八天:自定义转场动画以及设置titleView的状态
    swift项目第七天:构建访客界面以及监听按钮点击
    swift项目第六天:中间发布按钮的封装以及监听点击事件
    swift项目第五天:swift中storyBoard Reference搭建主界面
    swift项目第四天:动态加载控制器
    swift项目第三天:手写代码搭建主框架
  • 原文地址:https://www.cnblogs.com/Dreamice/p/13045550.html
Copyright © 2020-2023  润新知