IntelliJ IDEA uniformly sets the encoding to utf-8 encoding and SpringBoot jar package runs on the windows platform console and solves garbled logs

1. Background

The latest small program payment project is working, and there is no problem running in the local environment and linux environment deployment, but when deployed to the window environment, when requesting the Yitong payment interface, the other party returns

Try again later! io.vertx.core.json.Json.decodeValue(Json.java:168)
means that the encoding format is wrong, and the technical staff of the other party also said that there is a high probability that it is an encoding problem, and UTF-8 is required.

So I plan to spare an afternoon to thoroughly understand "Chinese garbled characters".

2. Knowledge preparation

  • The difference between ANSI, unicode and utf-8
    To make the computer support more languages, usually use 2 bytes in the range of 0x80~0xFFFF to represent 1 character. For example: the Chinese character '中' is stored in the two bytes [0xD6,0xD0] in the ANSI-encoded ANSI-encoded Chinese operating system.
    Different countries and regions have formulated different standards, resulting in their own encoding standards such as GB2312, GBK, GB18030, Big5, and Shift_JIS. These various Chinese character extension encoding methods that use multiple bytes to represent a character are called ANSI encoding. In Simplified Chinese Windows operating system, ANSI encoding represents GBK encoding; in Traditional Chinese Windows operating system, ANSI encoding represents Big5; in Japanese Windows operating system, ANSI encoding represents Shift_JIS encoding.
    Different ANSI codes are not compatible with each other. When information is exchanged internationally, text belonging to two languages ​​cannot be stored in the same ANSI coded text.
  1. The Chinese operating system defaults to ansi encoding
  2. unicode is an international universal encoding
  3. UTF-8 encoding is a "workaround" and "bridge" encoding for Unicode encoding when it is transmitted between networks (mainly web pages). UTF-8 can save data volume when transferring between networks.

3. The source code of the program runs through the processing stage

A piece of code to run to get the correct output, after the steps:

Java source code----Javac is compiled into a class bytecode file----Java virtual machine JVM loads and runs-operating system----display device.

Java source code—byte code: call the javac command of jdk to execute compilation, and javac uses the system character set by default. Usually we will set the file encoding UTF-8. Compiling with the command can also add -encoding UTF-8;

System.getProperty("file.encoding")   //可以获取当前系统使用的编码字符集

Java bytecode—virtual machine—operating system: What character set encoding is used to parse our class bytecode file when the virtual machine starts? At this time, we usually set -Dfile.encoding=utf-8 Here also explains the meaning of setting this VM parameter.

Operating system—display device: For the Chinese that appears in the encoding stage, in this step, the operating system needs to install Chinese fonts to support Chinese.

4. Problem description

When we run the jar package under Windows, there are often garbled characters, which are mainly divided into garbled characters in the log output by the DOS window and garbled characters in the data returned by the program.

  • Garbled characters appear in the log output by the dos window
    . Execute the following command to change the console output encoding to UTF8:
chcp 65001
  • The data returned by the program is garbled.
    Execute the following command, and specify the encoding as UTF8 when running the jar package:
java -Dfile.encoding=utf-8 -jar xxx.jar

Five, the solution

1. Modify the project encoding format to UTF-8

Setting>>Editor>>File Encoding
insert image description here

2. Change the encoding format in the encodings.xml file in the .idea folder of the project to uft-8

insert image description here

3.File->Settings->Build,Execution,Deployment -> Compiler -> Java Compiler

Set the Additional command line parameters option to -encoding utf-8
insert image description here

4. Then set to -Dfile.encoding=UTF-8 in Server > VM options

insert image description here

5. Configure servlet and tomcat coding in SpringBoot yml

server:
  port: 8082
  servlet:
    context-path: /api
    encoding:
      charset: UTF-8
  tomcat:
    uri-encoding: UTF-8

6. Configure log encoding UTF-8 in SpringBoot logback-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 日志存放路径 -->
    <springProperty scope="context" name="log.path" source="logging.file.path"/>
    <!-- 日志输出格式 -->
	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
    <property name="log.charset" value="UTF-8"/>
	<!-- 控制台输出 -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${
    
    log.pattern}</pattern>
            <charset>${
    
    log.charset}</charset>
		</encoder>
	</appender>

	<!-- 系统日志输出 -->
	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${
    
    log.path}/sys-info.log</file>
        <!-- 循环政策:基于时间创建日志文件 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志文件名格式 -->
			<fileNamePattern>${
    
    log.path}/sys-info.%d{
    
    yyyy-MM-dd}.log</fileNamePattern>
			<!-- 日志最大的历史 60-->
			<maxHistory>60</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>${
    
    log.pattern}</pattern>
            <charset>${
    
    log.charset}</charset>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 过滤的级别 -->
            <level>INFO</level>
            <!-- 匹配时的操作:接收(记录) -->
            <onMatch>ACCEPT</onMatch>
            <!-- 不匹配时的操作:拒绝(不记录) -->
            <onMismatch>DENY</onMismatch>
        </filter>
	</appender>

	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${
    
    log.path}/sys-error.log</file>
        <!-- 循环政策:基于时间创建日志文件 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志文件名格式 -->
            <fileNamePattern>${
    
    log.path}/sys-error.%d{
    
    yyyy-MM-dd}.log</fileNamePattern>
			<!-- 日志最大的历史 60-->
			<maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${
    
    log.pattern}</pattern>
            <charset>${
    
    log.charset}</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 过滤的级别 -->
            <level>ERROR</level>
			<!-- 匹配时的操作:接收(记录) -->
            <onMatch>ACCEPT</onMatch>
			<!-- 不匹配时的操作:拒绝(不记录) -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

	<!-- 用户访问日志输出  -->
    <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${
    
    log.path}/sys-user.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${
    
    log.path}/sys-user.%d{
    
    yyyy-MM-dd}.log</fileNamePattern>
            <!-- 日志最大的历史 60-->
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${
    
    log.pattern}</pattern>
            <charset>${
    
    log.charset}</charset>
        </encoder>
    </appender>

	<!-- 系统模块日志级别控制  -->
	<logger name="com.ruoyi" level="info" />
	<!-- Spring日志级别控制  -->
	<logger name="org.springframework" level="warn" />

	<!--系统操作日志-->
    <root level="info">
        <appender-ref ref="console" />
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    </root>

	<!--系统用户操作日志-->
    <logger name="sys-user" level="info">
        <appender-ref ref="sys-user"/>
    </logger>
</configuration>

7. Add UTF-8 encoding to the java -jar xxx command

java -Dfile.encoding=UTF-8 -jar xxxx.jar

The complete execution script under window:

@echo off
chcp 65001
java -Dfile.encoding=UTF-8 -jar ./api.jar 

Guess you like

Origin blog.csdn.net/u014212540/article/details/130637106