During our development, we often encounter various encoding garbled characters. In fact, this is a problem of inconsistent encoding. If you know the urlencoder encoding and urldecode decoding, then the problem disappears!
Let's take a look at the code about encoding:
Package COM. zhagnke. test; import Java. beans. encoder; import java.net. urldecoder; import java.net. urlencoder;/*** about urlencoder encoding and urldecode decoding * @ author spring sky * QQ 840950105 * Email vipa1888@163.com * my name: shi mingzheng **/public class encodeutil {public static void main (string [] ARGs) throws exception {/*** encoding ** if the character a-Z A-Z 0-9 or _, they will not be encoded */string str1 = "abcdefghijklmnopqrstyvwxyz"; string str1_1 = urlencoder. encode (str1, "UTF-8"); system. out. println ("str1_1 =" + str1_1);/*** encode ** non-character a-Z A-Z 0-9 or _, they are encoded */string str2 = "People's Republic of China"; string str2_2 = urlencoder. encode (str2, "UTF-8"); system. out. println ("str2_2 =" + str2_2); system. out. println ("--------------------------------");/*** decodes **/urldecoder UD = new urldecoder (); system. out. println (ud. decode (str1, "UTF-8"); system. out. println (ud. decode (str2, "UTF-8"); system. out. println ("---------------------------------");/*** decodes * If the character a-Z A-Z 0-9 or _, they are not compiled, so it will not be decrypted * if it is % E4 % B8 % ad % E5 % 9B % BD % E4 % Ba % E6 % B0 % 91% E5 % 85% B1 % E5 % 92% 8C % E5 % 9B % BD, they will be decoded as text */system. out. println (ud. decode (str1_1, "UTF-8"); system. out. println (ud. decode (str2_2, "UTF-8"); system. out. println ("---------------------------------");/*** if the decryption is not of the original character type (utf8 ----- GBK) */system. out. println (ud. decode (str1_1, "GBK"); system. out. println (ud. decode (str2_2, "GBK"); system. out. println ("---------------------------------");/*** if decryption is not the original character type (utf8 ----- ISO-8859-1) */system. out. println (ud. decode (str1_1, "ISO-8859-1"); system. out. println (ud. decode (str2_2, "ISO-8859-1 "));}}
The above is the code, and my comments have been written in it. If you do not understand it, please contact me and check the effect after running!
Str1_1 = abcdefghijklmnopqrstyvwxyzstr2_2 = % E4 % B8 % ad % E5 % 9B % BD % E4 % Ba % E6 % B0 % 91% E5 % 85% B1 % E5 % 92% 8C % E5 % 9b % BD please visit the People's Republic of China and abcdefghijklmnopqrstyvwxyz ä °? bytes
From the above, we can see that if the encoding is not ASCII, it will be compiled into a Hex Encoding. We only need to decrypt the compiled hex code! So in our daily life, if you encounter Garbled text, don't be flustered. First, check whether the encoding format in the database is consistent with that in the program. Only the encoding will never be garbled!
Learning lies in accumulation!