Writebytes () Problems

Source: Internet
Author: User

Httpurlconnection urlconn = (httpurlconnection) URL. openconnection ();

Urlconn. Connect ();

Dataoutputstream out = new dataoutputstream (urlconn. getoutputstream ());

String content = "questions about passing Chinese characters ";

Out. writebytes (content );

Out. Flush ();

Out. Close ();

Urlconn. Disconnect ();

According to the above method, the server must get a bunch of garbled characters, because: Out. writebytes (content); this statement has become garbled during conversion to Chinese.

Public final void writebytes (string s) throws ioexception {

Int Len = S. Length ();

For (INT I = 0; I <Len; I ++ ){

Out. Write (byte) S. charat (I ));

}

Inccount (LEN );

}

Because the char type in Java is 16-bit, a char can store a Chinese character. After converting it to a byte, the 8-bit high will be lost, in this way, Chinese characters cannot be completely output to the output stream. Therefore, it is best to convert the output of a Chinese character into a byte array first, and then write the data into the stream through write. Currently, this method has been tried: put out in the above link code. writebytes (content); Replace with out. write (content. getbytes (); first, convert the data into bytes in the write stream. The execution is successful and the server receives the correct Chinese content.

Related Article

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.