1.修改idea安装目录下的两个文件
C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\bin\idea.exe.vmoptions
C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\bin\idea64.exe.vmoptions
在这两个文件下增加一行:
-Dfile.encoding=UTF-8
2.做此修改后,tomcat运行日志会打印中文乱码
所以在还需要在VM options选项中配置:
工程乱码
打开File-Setting, 找到File Encodings这个选项,把encoding设置成你工程的编码即可,一般是UTF-8,如下图(红框的地方),然后重新rebuild一下,基本就行了
执行main函数时,控制台乱码
同样是打开setting,找到 Build,Execution,Deployment > Compiler > Java Compiler, 设置 Additional command line parameters选项为 -encoding utf-8,然后rebuild下,重新运行
运行tomcat时,控制台乱码
1)打开Run/Debug Configuration,选择你的tomcat
2)然后在 Server > VM options 设置为 -Dfile.encoding=UTF-8 ,重启tomcat
JSP乱码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> request和response request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
对POST乱码解决
<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>