Summary getBytes () contains garbled characters in the xml string caused by default encoding. getbytesxml

Source: Internet
Author: User

Summary getBytes () contains garbled characters in the xml string caused by default encoding. getbytesxml

The first impression of garbled code is how to unify the encoding. I seldom noticed that some of the processes interspersed during this period are also a major risk ..

A small program written by spring is deployed on sae, and the text messages from the cell phone always follow the test number and click the menu are garbled ..

Focus on some code for sending messages:

response.setCharacterEncoding("utf-8");response.getOutputStream().write(result.getBytes());

Encoding has been set to UTF-8, and spring also adds a filter for UTF-8 decoding... And the Console does print the sending information without garbled characters... I think this is definitely okay ..

Then I had to write a test code to get the data from the end:

Entity = new StringEntity (doc. asXML (); post. setEntity (entity); HttpResponse response = client.exe cute (post); String result = EntityUtils. toString (response. getEntity (); System. out. println ("result:" + result );

The result is garbled. If the receiver code is changed as follows, it will not be garbled:

String result = EntityUtils.toString(response.getEntity(),"gbk");

It proves that the garbled part is sent by gbk .. I am indeed a little blind. In the afternoon, my head has been ruined by the environment of sae .. I thought about it again. Since the code is garbled, it is normal during encoding, it may only be caused by a problem in getBytes, only the encoded string is supported .. let's take a look at the api description:

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. 

I fell down .. this makes me suddenly realize that I am a windows system, and the default is gbk. So here we will upload the part of Chinese according to gbk encoding, because we are response. setXX ("UTF-8"), so the receiver decodes the code according to UTF-8, directly garbled ..

So we should change it:

response.setCharacterEncoding("utf-8");
response.getOutputStream().write(result.getBytes("utf-8"));

Then the receiver can get Chinese without garbled characters without any processing ..

 

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.