According to the previous record in the Java Web Common coding garbled problem (a), and then record garbled case:
Case Analysis:
2, output stream write content or input stream read content garbled (Chinese in the content)
Cause Analysis:
A, if it is written in bytes or read garbled, you should first detect what the system encoding is to see if the Chinese-encoded character set is supported.
System.out.println (System.getproperty ("file.encoding"));
A1, if it is not supported in Chinese, it is necessary to set the JVM code, set the method:
The Windows environment is configured in Catalina.bat:
Set java_opts=%java_opts%-dfile.encoding=utf-8
Linux yellow Fine is configured in catalina.sh:
java_opts= "$JAVA _opts-dfile.encoding=utf-8"
After setting up, there should be no big problem. (Note: The read content is to be converted into a string, specifying a character set, such as:
A2, if it is supporting the Chinese character set, but also garbled words, the first thing to consider A1 in the note, and then change the character set for UTF-8, fully support Chinese,
There will be no loopholes.
b, if it is garbled by character write or read, then write and read should specify the same character set encoding (recommended is UTF-8). Example
//write characters by character StreamString charset = "UTF-8"; FileOutputStream Fout=NewFileOutputStream ("F:/test.txt"); OutputStreamWriter writer=NewOutputStreamWriter (Fout, CharSet);//Specify character set encodingWriter.write ("This is the age to fight, but to think too much, to do too little!") "); //read bytes converted into charactersFileInputStream FIS =NewFileInputStream ("F:/test.txt"); InputStreamReader Reader=NewInputStreamReader (FIS, CharSet);//Specify character set encodingStringBuffer content =NewStringBuffer (); Char[] buf =New Char[64]; intLen =-1; while(len = Reader.read (BUF))! =-1) {content.append (buf,0, Len); } System.out.println (Content.tostring ());
If you do not specify it will get the system code, it is possible to garbled, the solution can refer to a.
3, Database garbled
This is a very simple situation, you can view this blog post mysql insert data, the problem of Chinese garbled solution
From the above, garbled problem is not difficult to solve, to understand the source is good. There are two points to note, remind again:
1, it is best to specify a good system default encoding, or not set the encoding will get the current system encoding. (When you are not garbled in Windows, but porting to Linux can be garbled)
2. Set the Uriencoding property in tomcat and configure the global encoding filter to solve the problem that most of the request data is garbled.
Common coding garbled problem in Java Web (II.)