工程乱码
打开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
补充:
tomcat配置文件:在 tomcat / conf 目录下,设置 logging.properties
其他乱码
1)数据库数据是否乱码
查看数据库编码类型
show variables like ‘character%’
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /aebiz/soft/mysql/share/charsets/
2)jsp乱码
查看代码中是否添加下面代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
3)request和response
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
参考: