The Servlet sends the encoding of Chinese data to the client.

Source: Internet
Author: User

The Servlet sends the encoding of Chinese data to the client.

(For more information, see your subscription ID: it_pupil)

Get the string, encoded in a specific form into a byte array (such as a UTF-8 ). (You manually convert the byte array)

 

Tells the browser to decode the display (UTF-8) in the same way ).

 

If you are using a compaction stream, pay attention to three points:

 

The character stream is actually a character that you pass to it, and it will still be converted into bytes.

 

Get the string and tell the server that it is encoded as a byte array (such as a UTF-8) in a specific way at the time of sending ). (Byte arrays are converted internally by the swap Stream)

 

Tells the browser to decode the display (UTF-8) in the same way ).

 

As mentioned above, there are three implementation schemes to ensure no garbled code (also a solution to garbled code ):

 

Solution 1: byte stream:

 

 

Solution 2: streaming:

 

 

Solution 3: streams:

 


 

Solution 3 can simulate the encoding/decoding process as follows (the toHex method outputs the byte array in hexadecimal format ):

 

 

Therefore, the key of Solution three is that, at the beginning of the UTF-8 encoding, and finally Decoding with UTF-8, this is the essence.

 

Although the ISO-8859-1 is a single byte encoding, but, after starting with the UTF-8 encoding, you use the ISO-8859-1 decoding, it will be a byte to unlock, while the UTF-8 represents the Chinese characters, A Chinese Character consists of three bytes. Of course, it will contain garbled characters.

 

But it doesn't matter if it is garbled. This garbled code is only an intermediate process and does not need to be displayed. Followed by ISO-8859-1 re-encoding, then the encoded bytes are the same as the UTF-8 encoded bytes.

 

So, finally use the UTF-8 to decode, perfect!

 

 

This is the first article on encoding analysis:

Http://mp.weixin.qq.com? _ Biz = MzIyNzUzNjQ3MA ==& tempkey = 37C % 2F % 2BA4Bi9W % 2F7a0wo % %%%%%2bxxzpe3hd % %%2fejgyzg8vxrq % 3D % 3D & # rd

For more information, see related subscription numbers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.