The solution to the Chinese garbled problem caused by file.encoding

With the same code, the development environment is running normally, and the pages deployed to the production environment (regardless of Chinese or English letters) are also normal. When the server interface of the WeChat applet is called to send a template message to the WeChat official account under the same WeChat open platform, the production environment calls the interface to send messages in Chinese, and the development environment is normal when debugging.

1. Development environment

Operating system: win 10

Development tool: eclipse

Project file encoding (ie *.java file): utf-8

There are two ways to start:

① Run the main method of the JettyServer class in eclipse to start the embedded jetty

② Compile the project through ant, then package it into a jar, and finally start the embedded jetty by running the main method of the JettyServer class through the java command.

<javac includeantruntime="false" source="1.8" target="1.8" srcdir="${basedir}/src" destdir="${basedir}/build/classes" encoding="UTF-8">
System.out.println(System.getProperties().get("file.encoding"));

result:

① It is normal that the Chinese parameters passed by calling the interface when running and debugging through eclipse are normal, file.encoding=UTF-8

② When running the project through the java -jar command, access to the local page is normal, but the interface is called to send a message, Chinese garbled, file.encoding=GBK.

2. Production environment

Operating system: win 2008 r2

Startup method: run the project through the java -jar command

Result: Same as the development environment, accessing the local page is normal, but calling the interface to send messages, Chinese garbled, file.encoding=GBK.

Find the reason:

I didn't know that it was caused by file.encoding before, but I found out that it was because the file.encoding value was automatically consistent with the encoding format of the java file where the main method was located when eclipse started the project. When the java -jar command is started, the general file.encoding of the windows system is GBK, and the general file.encoding of the unix system is UTF-8.

Solution:

In the start.bat file of the startup project, add the parameter -Dfile.encoding=UTF-8. This solves the problem of Chinese garbled characters when calling the interface.

chcp 65001
java -jar -Dfile.encoding=UTF-8 xpl.jar
@cmd.exe

About the chcp command:

The default encoding method of cmd console of windows operating system is GBK.

Use the chcp 65001 command to change the encoding to UTF-8. This is only valid in the current console window. After closing the window, it will automatically restore to GBK.

Before file.encoding=GBK, the log information output by the console in the production environment is normal in Chinese. After -Dfile.encoding=UTF-8, the Chinese characters are garbled.

So add chcp 65001.

The strange thing is, I started the project after adding chcp 65001 in the development environment, the console output log Chinese is normal, and the console displays Chinese garbled characters in the production environment.

I don't know if it is the cause of win 2008 r2.

 

Guess you like

Origin blog.csdn.net/xiaozaq/article/details/97642111