Problems with garbled code processing

Source: Internet
Author: User

Sometimes, during development, we cannot ask the customer to change the character set. Recently, a Chinese character problem is quite annoying. We tested it on IE and Firefox and passed the new string (keyworld. getbytes ("iso-8859-1"), "GBK"); conversion can decode Chinese on the URL, but it cannot be parsed on the 360 browser; Use the 360 browser through new string (keyworld. getbytes ("iso-8859-1"), "UTF-8"); can be decoded successfully, and it is garbled on IE and Firefox.

After setcharacterencoding is set on the servlet, it really hurts me for a while.

request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=UTF-8");
After checking that Java does not provide string-based verification of garbled characters, we will try to use regular expressions to exclude special characters produced by garbled characters. Although this is not strictly prohibited, I personally think it is okay to deal with general garbled issues.

<Span style = "white-space: pre"> </span> string keyworld = request. getparameter ("keyword"); string kW = new string (keyworld. getbytes ("iso-8859-1"), "GBK"); string kw2 = new string (keyworld. getbytes ("iso-8859-1"), "UTF-8"); keyworld = keyworld! = NULL? Keyworld. trim (). tolowercase (): ""; string Reg = "^ [A-Za-z0-9 \ u4e00-\ u9fa5] + $"; // regular character, verify the special identifier pattern = pattern. compile (REG); matcher = pattern. matcher (keyworld); matcher matcher2 = pattern. matcher (kW); matcher matcher3 = pattern. matcher (kw2); If (matcher. find () {system. out. println ("garbled 1:" + keyworld);} else {If (matcher2.find () {system. out. println ("garbled GBK format conversion successful:" + kW); keyworld = kW;} else {system. out. println ("garbled, GBK Format Conversion failed:" + kW); If (matcher3.find () {system. out. println ("garbled UTF-8 format conversion succeeded:" + kw2); keyworld = kw2;} else {system. out. println ("garbled, non-UTF-8 and GBK encoding Formats") ;}} system. out. println ("keyworld:" + keyworld );
The following is an example:

Garbled characters are successfully converted to GBK format: Garbled characters exist in the east, and GBK format conversion fails: Juan? UTF-8 format conversion success: East
Generally, garbled characters are generated after the conversion fails ??? Some of them will be ?? , Etc.


In fact, there is another method that is most practical. We strongly recommend that you use the urlencoder and urldecoder classes in the Java package to add Chinese parameters to URL parameters and decode them in the background, it is best to add a character set in the Process of encryption and decoding.

Example: system. Out. println (urlencoder. encode ("Fortune", "GBK"); // result % B7 % A2 % B2 % C6 % C1 % CB
System. out. println (urlencoder. encode ("Fortune", "UTF-8"); // result % E5 % 8f % 91% E8 % B4 % A2 % E4 % Ba % 86


In addition, because it has encountered such a problem, the following uses the get method in httpclient 4.3 to obtain the content, at this time, although the address bar of the browser can be directly output, but the httpclient in the background does not get data, this is because some browsers can directly transcode Chinese. Therefore, httpclient can be simply understood as a browser that requires manual Chinese transcoding.

Closeablehttpclient httpclient = NULL; try {httpclient = httpclientbuilder. Create (). Build (); string url = "http: // localhost: 8080/dcmapservice/servlet/proxyservlet? Type = adder_query & request = Key & keyword = "+ urlencoder. encode ("West", "GBK") + "& START = 0 & num = 2"; httpget = new httpget (URL); httpresponse response = httpclient.exe cute (httpget ); system. out. println (httpget. geturi (); httpentity entity = response. getentity (); // determines whether the response object is null if (entity! = NULL) {system. out. println ("\ n query" West ", response content:" + entityutils. tostring (entity, "GBK") ;}} catch (exception e) {e. printstacktrace ();} finally {try {// close or release the resource httpclient. close ();} catch (ioexception e) {e. printstacktrace ();}}






Problems with garbled code 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.