各种中文乱码解决办法

在编程的时候会遇到各种中文乱码,这里进行统计以便以后查阅

1、前端页面元素中文乱码

 <meta http-equiv="Content-Type" content="text/html; charset=GBK" />

会出现下面乱码

页面上的元素也就是html内的元素,是中文的会出现乱码,而从后台获取的中文不会出现乱码。

解决方法:页面上设置编码方式为UTF-8

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

2、URL传参、get方式传参出现中文乱码,如下

出现这种情况,要先确定参数在前台页面上不是乱码的,可以alert()一下,看参数是否乱码

解决办法1:

对于以get方式传输的数据,request默认使用ISO8859-1这个字符编码来接收数据,客户端以UTF-8的编码传输数据到服务器端,而服务器端的request对象使用的是ISO8859-1这个字符编码来接收数据,服务器和客户端沟通的编码不一致因此才会产生中文乱码的。

解决办法:在接收到数据后,先获取request对象以ISO8859-1字符编码接收到的原始数据的字节数组,然后通过字节数组以指定的编码构建字符串,解决乱码问题。

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        String id= request.getParameter("id");
        id=new String(name.getBytes("ISO8859-1"), "UTF-8") ;
}

解决方法2:

修改tomcat服务器的编码方式,可以在server.xml里面设置

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

设置成红字部分,但是有时也是不可用的,因为即使这里设置的是UTF-8但是其他地方设置成其他编码方式会覆盖掉这个设置,仔细检查各个地方的编码。

 比如Spring Boot 的application.properties配置文件里设置成server.tomcat.uri-encoding=GBK就会覆盖掉tomcat自己的设置,我这里是打个比方,因为SpringBoot是内置Tomcat服务器的。

解决办法3:中文参数进行编码处理
?id="+encodeURI(encodeURI("中文参数"));
后台:
String name = request.getParameter("name");
String str = URLDecoder.decode(name,"UTF-8");

3、POST方式出现中文乱码

原因:因为服务器和客户端沟通的编码不一致造成的,因此解决的办法是:在客户端和服务器之间设置一个统一的编码,之后就按照此编码进行数据的传输和接收。

解决方法:由于客户端是以UTF-8字符编码将表单数据传输到服务器端的,因此服务器也需要设置以UTF-8字符编码进行接收

1、后台代码

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//注意要在getParameter之前设置
     String id= request.getParameter("id");
}

2、如果使用的是框架的话,可以统一设置字符过滤器,这里以 SpringMVC为例:

<filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <description>字符集编码</description>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
     </filter>
     <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
     </filter-mapping>

3、SpringBoot 这样设置: 创建一个类继承WebMvcConfigurerAdapter 

public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

}

4、使用注解@RequestBody 导致接收的中文参数乱码,可以参考我的这篇博客(比较详细)https://www.cnblogs.com/lwx521/p/9855891.html



原文地址:https://www.cnblogs.com/lwx521/p/9856186.html