专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

中文乱码问题汇总(控制台、response、get和post、过滤器处理乱码、springmvc乱码)

1、IDEA控制台乱码

(1)在书写普通的java程序输出中文的时候,不会出现控制台乱码的问题:

public class Test {
    public static void main(String[] args) {
        System.out.println("劳动光荣");
    }
}
劳动光荣

(2)在servlet输出中文的时候,控制台出现乱码的问题:

public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           System.out.println("劳动光荣");
    }
}
[2020-05-01 01:21:20,094] Artifact mvc_01:war exploded: Artifact is deployed successfully
[2020-05-01 01:21:20,095] Artifact mvc_01:war exploded: Deploy took 3,243 milliseconds
�Ͷ�����

(3)解决方案:

在IDEA中作如下配置,进入IDEA如下图的配置页面:

93_1.png

在此行添加编码格式:

93_2.png

控制台输出中文正常:

[2020-05-01 01:23:02,634] Artifact mvc_01:war exploded: Artifact is deployed successfully
[2020-05-01 01:23:02,634] Artifact mvc_01:war exploded: Deploy took 4,541 milliseconds
劳动光荣

2、response中文乱码

(1)要确定代码的编码格式为UTF-8

(2)乱码原因:浏览器和服务器的编码格式不同:

服务器的默认编码为:ISO-8859-1,如果浏览器的编码不是ISO-8859-1,就会出现乱码:

 public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        response.getWriter().write("你好");
    }

93_3.png

(3)解决方法:

加入代码:

    response.setCharacterEncoding("UTF-8");//设置服务器的编码,默认是ISO-8859-1
    response.setContentType("text/html; charset = utf-8");//告诉浏览器服务器的编码格式

93_4.png

可以正常显示。

3、get和post提交中文乱码(https://tech.souyunku.com/zhai1997/p/12489507.html

4、过滤器解决中文乱码问题

(1)使用表单采取get方式提交:

表单:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/hello" method="get">
      <input type="text" name="name">
      <input type="submit">
    </form>
  </body>
</html>

springmvc的处理器:

@Controller
public class HelloController{
    @GetMapping("/hello")
      public String hello(String name,Model model){
          model.addAttribute("msg",name);
          return "test";
      }
}

接收数据的表单:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

接收到的数据未出现乱码现象

93_5.png

(2)采用post方式提交:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/hello" method="post">
      <input type="text" name="name">
      <input type="submit">
    </form>
  </body>
</html>
@Controller
public class HelloController{
    @PostMapping("/hello")
      public String hello(String name,Model model){
          model.addAttribute("msg",name);
          return "test";
      }
}

93_6.png

(3)采用过滤器解决乱码问题:任何请求都会经过该过滤器

书写过滤器的代码:

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {

    }
}

在web.xml中配置过滤器:

    <filter>
        <filter-name>Encoding</filter-name>
        <filter-class>pers.zhb.filter.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

测试:

93_7.png

5、springmvc解决中文乱码问题(get方式乱码,post方式不会)

get方式提交并采用get方式接收数据:

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @GetMapping("/t1")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("msg","hhhha");
        return "test";
    }
}

不需要自己书写过滤器,直接在web.xml中配置过滤器即可:

<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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

测试:

93_8.png

文章永久链接:https://tech.souyunku.com/22489

未经允许不得转载:搜云库技术团队 » 中文乱码问题汇总(控制台、response、get和post、过滤器处理乱码、springmvc乱码)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们