Java language default encoding method is Unicode, and our Chinese usually used files and databases are encoded based on GB2312 or BIG5, how can they properly select the Chinese character encoding mode and correctly handle the code of Chinese characters? This article will start from the common sense of Chinese character encoding, combined with Java programming instance, analyze the above two questions and solve their solutions:
1. Add a statement in the JSP program:
<% @ Page ContentType = "text / html; charset = GB2312"%>
2. In the URL, please show the encoding problem of strings.
If the information passed from the client passed through the GET / POST method, servlet / JSP cannot get the correct value.
We specify the encoding mode desired before calling Request.GetParameter ("param_name").
That is, Request.SetCharacteRecoding ()
3. The problem with different platform codes is different.
The standard on the Linux platform is ISO8859_1, while Win2k is GBK, these are the default standards, if your server is not like this, then compiled issues will have problems. I have encountered such a problem, two Linux servers, one lang = en, one lang = en, utf8, I have found the reason for I for a long time.
3. I use the most converted coded class (in the Linux platform), almost all coding problems can be solved. The way the class is very resolved. Mainly two classes of ASC2GB () and GB2ASC ().
Package com.whaic.tools;
Import java.io.unsupportedEncodingexception;
Public Class EcoV
{
PUBLIC STATIC STRING ASC2GB (String ASC) {
String ret;
IF (ASC == Null) Return ASC;
Try {
RET = New String (asc.getbytes ("ISO8859_1"), "GB2312");
}
Catch (unsupportedencodingexception e) {
RET = ASC;
}
Return Ret;
}
Public Static String GB2ASC (String GB) {
String ret;
IF (GB == NULL) RETURN GB;
Try {
RET = New String (GB.GetBytes ("GB2312"), "ISO8859_1");
}
Catch (unsupportedencodingexception e) {
RET = GB;
}
Return Ret;
}
}
4. Chinese issues when reading and writing documents:
Read ::
FileInputStream FII = New FileInputStream (Strinfile);
InputStreamReader ISR = New InputStreamReader (FIS, "GB2312");
Reader in = New BufferedReader (ISR);
int CH;
While (ch = in.read ())> -1) {
iCharnum = 1;
Buffer.Append ((char) CH);
}
In.Close ();
Write ::
FileOutputStream Fos = New fileoutputStream (stroutfile); Writer out = new OutputStreamWriter (FOS, "BIG5");
Out.write (STR);
Out.close ();
The above is just some of the issues and solutions encountered in everyday use. Since the work of internationalization is not completed in China, there is no strict test before these basic classes, so the support of Chinese characters is not as perfect as Java Soft claims. Java programming languages grow in the network world, which requires Java to have good support to multi-country characters. The Java programming language adapted to calculate the needs of networked, and laid a solid foundation for it to grow rapidly in the network world. Java Soft has taken into account the support of the Java programming language to multi-country characters, just now there are many defects in the current solution, and we need some compensatory measures. The World Standardization Organization is also trying to unify all the words of human beings in a code, one of which is ISO10646, which uses four bytes to represent a character. Of course, before this solution is not adopted, it is desirable that Java Soft can strictly test its product to bring more convenience to users.