In Java-based programming, we often encounter problems with the processing and display of Chinese characters, such as a lot of garbled characters or question marks.
This is because the default encoding method in JAVA is UNICODE
, and the files and DBs commonly used by Chinese people are based on GB2312
or BIG5
and other codes, so this problem will occur. Below is a summary of such issues.
Free learning video sharing: java video
1. Output Chinese in the web page
JAVA is used in network transmission The encoding 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 when compiling the program, the encoding used is "GB2312
", and if you run this program on a Chinese platform, this problem will not occur, so be sure to pay attention.
2. Read Chinese from parameters
This is exactly the opposite of outputting on the web page, such as:
str=new String(str.getBytes("8859_1"),"GB2312");
3. Operation DB Chinese problem in
A simpler method is: in the "Control Panel", set the "Region" to "English (United States)". If garbled characters still appear, you can also make the following settings:
When fetching Chinese:
str=new String(str.getBytes("GB2312"));
Input Chinese into DB:
str=new String(str.getBytes("ISO-8859-1"));
4. In jsp Chinese solution
In the "Control Panel", set the "Region" to "English (United States)".
Add in the JSP page:
If it is not displayed normally, the following conversion must be performed:
For example:
name=new String(name.getBytes("ISO-8859-1"),"GBK");
Then there will be no Chinese problem.
For more related articles and tutorials, please visit: java introductory tutorial
The above is the detailed content of Summary on the problem of question marks and garbled characters in java. For more information, please follow other related articles on the PHP Chinese website!