Modify jvm encoding problem at Tomcat runtime

  • 2021-07-22 11:55:38
  • OfStack

Question:

Recently, when deploying the project, the data is garbled. After checking the project once, it is in UTF-8 coding format, and the data is also in it, but it is garbled when it is transmitted to the other party through the calling interface.

Because it is deployed in Windows environment, Windows encodes GBK by default, which leads to the problem that jvm runtime encoding is not 1, and it is easy to modify.

Solution:

If the UTF-8 encoding is used in the Linux environment, it is not necessary to modify it, because Linux defaults to UTF-8

The war package is deployed on Tomcat

Modify jvm encoding

Linux Environment

Configuration in catalina. sh

JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=utf-8"

Because the default encoding format in the Windows environment is GBK, the Tomcat runtime encoding format will be modified.

Windows Environment

Configuration in catalina. bat

set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8

If it is an jar package deployment, you only need to add jvm parameters to the startup command.

jar package deployment

Start command to add jvm parameter

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

Summarize

The above is this site to introduce the modification of Tomcat runtime jvm coding problem, I hope to help you!


Related articles: