Encoding problem with URLDecoder in Java


java.net.URLDecoder.decode

  I encountered a strange problem in the project, that is, I used the java.net.URLDecoder.decode(ruleName) method to decode locally. There is no problem, and the local page can be opened normally. But when I ported the code to the test environment, I couldn't open the page, and there was no error message when I checked the background log.

  JDK1.6 used in the local environment, 7 used by tomcat, and the tomcat version of JDK1.6 in the test environment is not clear

I was wondering, because this method has been prompted out of date, I was thinking about whether it was caused by this problem, there should be an alternative method, and then I went to check the JDK API, as follows

  Try to change the java.net.URLDecoder.decode(ruleName) in the code to java.net.URLDecoder.decode(ruleName, "UTF-8"); Then compile and replace the code of the test environment, and find that the problem is solved.

  This should be because some outdated codes in the JDK may be invalid in the lower version of tomcat, so everyone must try to avoid the use of outdated methods in future programming to avoid unnecessary problems.



Case 2:

I will play Java Web with my friends. My friends are in charge of the front end and I am in charge of the back end. The front end uses jQuery to interact with the back end, and the parameters and responses are all passed in JSON. When passing parameters, the JSON string will definitely be URL Encoded, and can only be parsed after URL Decode is performed on the backend. At the beginning, URLDecoder was used to parse:

[java]  view plain copy  
  1. return URLDecoder.decode(str);  

Then I found out that all the Chinese characters are garbled... It's embarrassing. At first, I thought that the encoding of the data sent by the browser was different from the default encoding of Java, so various conversion encodings were still kneeling. Judging the encoding, I found that it was originally UTF-8, but it was still garbled when printed. Later, I printed the string in bytes, and found that the Chinese part was exactly the same as the part of the string after URL Encode. I saw someone use URL Encode and then Decode to solve the problem of Chinese garbled characters, only to realize that there was a problem with URL Decode. . In fact, the URLDecoder.decode(String) method is deprecated, plus the parameters of the specified text encoding, the garbled problem is solved:


[java]  view plain copy  
  1. return URLDecoder.decode(str, "UTF-8");  
It seems that the deprecated method should not be used in the future... There must be a reason for the abandonment orz

By the way, paste the method of judging string encoding in Java:

[java]  view plain copy  
  1. publicstatic String getEncoding(String str) {         
  2.         String[] encode = {  
  3.                 "GB2312""ISO-8859-1""UTF-8""GBK"  
  4.         };  
  5.         for(int i = 0; i < encode.length; i++) {  
  6.             try {  
  7.                 if(str.equals(new String(str.getBytes(encode[i]), encode[i]))) {  
  8.                     return encode[i];  
  9.                 }  
  10.             } catch (UnsupportedEncodingException e) {  
  11.                   
  12.             }  
  13.         }  
  14.         return"";   
  15.     }  

I almost vomited writing Java. If I have time, maybe I should learn Python. After all, life is short.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325709949&siteId=291194637