Various Chinese garbled solutions in Java

1. Servlet output garbled characters

1. Use the servlet.getOutStream byte stream to output Chinese, assuming that the output is String str = "Diaoyu Island is Chinese, Shameless is Japan".

1.1 If it is a local server and a local client, it goes without saying that out.write(str.getBytes()) can be output without any problem. Because str.getBytes() is used in the server to use the default local encoding, such as GBK. And the browser also uses the local default encoding when parsing, the two are unified, so there is no problem.

1.1 If the server is used for output, out.write(str.getBytes("utf-8")). When the local default encoding is GBK (the ratio is in China), it will be garbled when opened with a browser. Because the server sends the 1010 data of utf-8, and the client browser uses gbk to decode, the two encodings are not unified, and it must be garbled. Of course, you can also manually call the encoding of the client browser yourself (IE menu is: Query View->Encoding encoding->utf-8), but this kind of operation sucks, it is best to tell the server to output the response header, Which encoding the browser uses to decode. So in the servlet of the server, add response.setHeader("content-type","text/html;charset=utf-8"), of course, you can also use simple response.setContentType("text/hmtl;charset= utf-8"). Both operations are the same.

2. Use the servlet.getWirter character stream to output Chinese, assuming that the output is String str = "Diaoyu Islands are Chinese, Shameless is Japan".

2.1 If it is written as out.print(str) for output, the client browser will display multiple characters of ????, which means that the corresponding characters cannot be found in the encoding table to display. The reason is: the character output stream obtained by servlet.getWriter() uses ISO-8859-1 for character output by default, and ISO-8859-1 definitely does not support Chinese. So the first thing that must be done first is that the output characters of the server object can support Chinese. Secondly, the response header written by the server to the client should tell the client which encoding table is used for encoding. To achieve these two requirements, only response.setContentType("text/hmtl;charset=utf-8") is needed. That's it. Special attention: response.setContentType("text/html;charset=utf-8") should be placed in front of the PrintOut out = response.getWriter() code, otherwise it only has the function of telling the client what code table encoding is used, and the server The terminal is still encoded with ISO-8859-1. Another special reminder: in the doGet or doPost methods in the same Servlet, you cannot use both response.getOutputStream and response.getWriter, because the response output byte stream and character stream of these two responses conflict, you can only use their one.

Second, Servlet file download, Chinese garbled situation.

The key is attachment;filename=filename in the response header content-disposition when downloading. The filename filename cannot contain Chinese character strings, it must be encoded with URLEncoding encoding before HTTP transmission can be performed. The following code example:

 

 

3. Add addCookie to the Servlet response, and solve the problem of the Chinese code of the value in the cookie.

For the principle of cookies, see http://blog.csdn.net/chenshufei2/article/details/8009992 . If you want to store the Chinese value in the cookie, you must encode it with Base64 and send it to the client's browser for storage. The next time the client browses, the value in the cookie brought back is Base64 encoded, so it needs to be decoded with Base64. Base64 encoding mainly solves the problem of re-encoding special characters into ab, AB, 0-9, + and /, 52 characters, 10 numbers and a +, and a / for a total of 64 characters. Its principle is to encode the content of the original 3 bytes into 4 bytes. Mainly, after taking the 6 bits of the byte, add 00 in front to form a new byte. So the original 3 bytes total 24, which are encoded into 4 bytes of 32 bits.

Specific code examples are as follows:

 

Fourth, get request parameters garbled

The garbled code of the GET method:

For example, <a href=”/demo5/servlet/RD2?name=China”>CN</a>, the string strCN obtained directly by request.getParameter will be garbled. This is also because the GET method is sent by http url The default is encoded with iso-8859-1, so the strCn obtained first should be encoded with iso-8859-1 to get the original text, and then use utf-8 (see what charset of the specific page is utf-8 or gbk) to carry out Just decode it. new String(strCn.getBytes("ISO-8859-1"), "UTF-8");

The troublesome thing to do in this way is that there is a parameter that needs to be encoded once and then decoded by iso-8859-1.

Garbled characters in the POST method: only request.setCharacterEncoding("UTF-8"): is required.

Guess you like

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