Java garbled problem Summary

Source: Internet
Author: User

In Java-based programming, often encountered in the Chinese characters and display problems, such as a lot of garbled or question marks.
This problem occurs because the default encoding in Java is Unicode, and the files and db commonly used by the Chinese are based on encodings such as GB2312 or BIG5. I used to make sure that the current page and the target page are consistently encoded to solve most of the problems. But again in the cause of this problem, because the target page could not be encoded (for example, using the Org.glassfish.jersey rest WebService), and then after a lot of data, finally solved, I know there must be a lot of friends will encounter this problem, So special summed up a bit, to come out and let everyone share.

For the WS problem above, you must manually transcode:

1, the output of Chinese in the Web page.
Java is used in network transmission of the encoding is "iso-8859-1", so in the output need to convert, such as:
String str= "Chinese";
Str=new String (str.getbytes ("GB2312"), "8859_1");
However, if you compile the program, the encoding used is "GB2312", and the Chinese platform to run this program, do not appear this problem, it must be stressed.
2. Read Chinese from the parameters
This is exactly the opposite of outputting the output in the Web page:
Str=new String (str.getbytes ("8859_1"), "GB2312");
3, the operation of the Chinese problem in the DB
A simpler approach is to set the area to English (United States) in control face wrench. If there are garbled characters, you can also set the following:
When taking Chinese: str=new String (Str.getbytes ("GB2312"));
Input Chinese to DB: Str=new String (Str.getbytes ("iso-8859-1"));

Java garbled problem Summary

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.