The solution of Chinese garbled characters in Java communication process

J In Java-based programming, we often encounter problems with the location and display of Chinese characters. Like a bunch of gibberish or question marks.

This is because the default encoding in JAVA is UNICODE. However, the files and DBs commonly used by Chinese are based on encodings such as GB2312 or BIG5, so this problem occurs.

Assuming that the file is garbled as soon as it is opened, this problem can be considered by changing the encoding of the software or changing the encoding of the file.

And if it is in java communication. Or when other software processes such as database operations communicate, it is easy to generate garbled characters.

1. Output Chinese in the web page.

The encoding used by JAVA in network transmission is "ISO-8859-1", so it needs to be converted when outputting, such as:

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

But if the code used is "GB2312" when compiling the program, and the program is executed on the Chinese platform. This problem does not occur. Be sure to pay attention.

2. Read Chinese from parameters

This is just the opposite of outputting in a web page like:

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

3. Operate Chinese problems in DB

A simpler method is: in the "Control Panel", set the "Region" to "English (United States)". If there are still garbled characters, you can also perform the following settings:
When taking Chinese: str=new String(str.getBytes("GB2312"));
Input Chinese into DB:str=new String(str.getBytes("ISO-8859-1"));

4. Chinese solution in jsp:

In the "Control Panel", set the "Region" to "English (United States)".
Increase in the JSP page:
If it still doesn't work, it will display normally. Then the following conversions must be performed:
For example, name=new String(name.getBytes("ISO-8859-1"),"GBK");
there will be no Chinese problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324716678&siteId=291194637