1. 为什么不支持中文

1.1 tomcat

​ tomcat在conf/logging.properties文件中定义tomcat输出为utf-8编码,在conf/server.xml中未指明请求编码方式,而使用默认编码ISO-8859-1

conf/loggin.properties

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.maxDays = 90
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8

conf/server.xml

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"/>

1.2 CMD or PowerShell

windowsCMD和PowerShell的默认编码为

image-20201012100233416

可通过CHCP 查看和修改当前命令行的编码格式(仅作用于当前窗口),另windows系统默认为GBKimage-20201012100653631

1.3 IDEA

IDEA的字符编码随文件、系统、项目、JDK的不同而不同(多处配置编码但一般为utf8),除tomcat控制台外默认为UTF-8(不考虑远古版本)。

IDEA内置的tomcat控制台经过二次编码处理,所以tomcat控制台和IDEA的tomcat控制台编码不能等价

2. 解决方法

2.1 判断哪里乱码(tomcat还是IDEA)[仅限控制台]

单独启动tomcat查看tomcat打开的控制台是否是乱码,如果乱码代表tomcat控制台从开始就已经乱码,设置编码由tomcat到IDEA,IDEA一般情况默认编码已经是utf-8了,除了控制台和终端使用gbk

2.1 最少修改

修改tomcat的conf/loggin.properties中的所有UTF-8为GBK 使控制台编码正常

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90
1catalina.org.apache.juli.AsyncFileHandler.encoding = gbk

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
2localhost.org.apache.juli.AsyncFileHandler.encoding = gbk

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.maxDays = 90
3manager.org.apache.juli.AsyncFileHandler.encoding = gbk

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90
4host-manager.org.apache.juli.AsyncFileHandler.encoding = gbk

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = gbk

修改tomcat的conf/server.xml,使请求信息为UTF-8(与servlet处理编码方式一致,可根据实际情况更改)

    <Connector port="8086" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URLEncoding="utf-8" />

IDEA的各个地方设置UTF-8:

  1. 编译器

    image-20201012101823344

  2. 文件默认编码及当前文件编码

    image-20201012101927771

    右下角

    image-20201012102009792

    1. tomcat VM

      image-20201012102206284

    2. IDEA本身编码,idea目录下两个vm文件追加-Dfile.encoding=UTF-8

      image-20201012102242809

    3. 当前项目编码设置,.idea/encoding.xml中设置"UTF-8"

    4. 什么你还没解决,你电脑是个奇行种,还是设置统一编码吧。

2.2 统一编码

各个地方编码不统一 出错哪一环都不知道,就要祭出大杀器了,所有设置为UTF-8,会引发一些对windows编码强制设置gbk的软件的编码问题(一般是非主流软件)例如某小工具:

image-20201012103218133

食用方法:

  1. 上2.1中一样的位置gbk改为utf-8,如果使utf-8就不变

  2. 解决CMD编码(永久)

    1. 修改注册表(win+R输入regedit)HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe,新建一个 DWORD(32位值),命名为CodePage,值设为65001 已有CodePage的话,修改它,改为十进制,65001;

      修改注册表HKEY_CURRENT_USER\Console\Tomcat,新建一个 DWORD(32位值),命名为CodePage,值设为65001 已有CodePage的话,修改它,改为十进制,65001。注:如需设置powershell 同目录下含有powershell的项执行相同操作

    2. 或新建cmd.reg

      Windows Registry Editor Version 5.00
      [HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
      "CodePage"=dword:0000fde9
      "FontFamily"=dword:00000036
      "FontWeight"=dword:00000190
      "FaceName"="Consolas"
      "ScreenBufferSize"=dword:232900d2
      "WindowSize"=dword:002b00d2
      

      然后执行

  3. 设置windows编码

    1. 打开小娜搜索控制面板,选择时钟与区域,点击区域,点击管理,带点击更改系统区域设置,勾选Bate版:使用Unicode UTF-8提供全球语言支持,然后立即重启(必须)

      image-20201012104701659

  4. IDEA设置控制台二次编码格式

    image-20201012104336599

如果提示不存在就新建

image-20201012104420246

image-20201012104411106

至此,所有编码均统一为utf-8,前后端声明时应声明utf8编码即可解决所有中文乱码

  1. IDEA设置可能需要重启生效,或未清除缓存,可选择清除缓存并重启

    image-20201012104927280

如果没有解决,请回头看看,是不是看的太快遗漏了,,如果没有遗漏,请选择重启,重装重买.


努力决定态度,态度决定高度.