Garbled in Java

Source: Internet
Author: User
Tags http post

Java development often encountered garbled problems. Online to provide a variety of solutions to solve the problem, such as HTTP request the Chinese garbled problem, can be solved by Urlencoder and urldecode, although the problem solved, but not elegant, you will find to solve the garbled problem, the code added a few lines of code, And the request side and the response side to add Urlencoder and Urldecoder code, both sides of the staff are not happy, have specified the character set, but also to transcode, this is not elegant.

In fact, to solve the garbled problem is very simple, garbled occurrences of the place is definitely a data flow operation, we need to uphold a principle of "what character set to write, should use what character set to read."

Common Chinese garbled solution:

1.HTTP POST request Chinese characters can be set by the filter to the HttpRequest character sets.

2.HTTP GET request Chinese garbled solution There are two kinds, one is new String (Request.getparamter ("name"). GetByte ("Iso-8859-1"), "UTF-8"); Is the character set that specifies the URL request through the Web server.

3.Response can be passed response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("application/html; Charset=utf-8 ") to set the character set of the response information

4. The last situation often makes people crazy, clearly set the response character sets, but still garbled. Oh, ya, printwriter out = Response.getwriter (); need to be behind 3 steps. This means that the character set of the response information is specified first, and then the output stream is obtained based on the character set, otherwise the default character stream of the response is iso-8859-1.

5. File operation, remember to write the character set of the stream, read out the stream to use write-time character set, solve the Chinese garbled problem.

Summary: With what character set to write, you need to use what character set to read.

to say a few more, online many put forward a new string (Val.getbyte ("iso-8859-1"), "GBK"); this thing is often fraught, if your Val string is actually a string that has been based on the specified character set, Solve garbled problem or adhere to the principle, traced! Resources on the web need to be carefully screened.


Garbled in Java

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.