Java solves the problem of Chinese garbled codes in the communication process

  • 2020-06-01 09:54:15
  • OfStack

Java solves the problem of Chinese garbled codes in the communication process


In the programming of Java, we often encounter the problem of Chinese characters' processing and display, such as a lot of scrambled codes or question marks.

This is because the default encoding method in JAVA is UNICODE, while the files and DB commonly used by Chinese people are based on GB2312 or BIG5, so this problem will occur.

If file 1 is open to garble code, you can modify the software code or modify the file code can feel this problem. However, if it is in the communication of java, or other software processes such as database operation, it is easy to generate scrambled codes.

1. Output Chinese in web pages.

The code used by JAVA in network transmission is "ISO-8859-1", so it needs to be transformed in the output, such as:

String str=" Chinese "; 
str=new String(str.getBytes("GB2312"),"8859_1"); 

But if be in when compiling program, the code that USES is "GB2312", and run this program on Chinese platform, won't appear this problem, 1 must want to notice.

2. Read Chinese from the parameters

This is just the opposite of the output in the web page:

str=new String(str.getBytes("8859_1"),"GB2312");

3. Operate the Chinese questions in DB

An easier way to do this is to set the "area" to "English (USA)" in the "control panel". If still can appear garbled code, still can undertake the following setting:

When taking Chinese: str=new String(str.getBytes ("GB2312"));

Enter Chinese into DB: str=new String(str.getBytes (" ISO-8859-1 ");

4. Chinese solution in jsp:

In the control panel, set the "zone" to "English (USA)".

Add to the JSP page:

If not normal display, then the following conversion:

Such as: name = new String (name getBytes (" ISO - 8859-1 "), "GBK");

There would be no problem with Chinese.

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: