The Chinese transcoding tool obtained from the Internet
Package Com.test;import Java.io.unsupportedencodingexception;import Java.net.urlencoder;public class CharToolsUtil { public static final String Utf8urlencode (string text) {StringBuffer result = new StringBuffer (); for (int i = 0; i < text.length (); i++) {char c = text.charat (i); if (c >= 0 && C <= 255) {result.append (c); }else {//transcode Chinese byte[] b = new Byte[0]; try {b = character.tostring (c). GetBytes ("UTF-8"); }catch (Exception ex) {} for (int j = 0; J < B.length; J + +) {int k = b[j]; if (k < 0) k + = 256; Result.append ("%" + integer.tohexstring (k). toUpperCase ()); }}} return Result.tostring (); }/** * Utf8url decode * @param text * @return * Single Chinese characters are transcoded to%e with a starting length of 9 */public static final string Utf8urldeco De (string text) {string result = ""; int p = 0; if (Text!=null && text.length () >0) {text = Text.tolowercASE (); p = text.indexof ("%e"); if (p = =-1) return text; while (P! =-1) {result + = text.substring (0, p); Text = Text.substring (P, text.length ()); if (Text = = "" | | Text.length () < 9) return result; Result + = Codetoword (text.substring (0, 9)); Text = text.substring (9, Text.length ()); p = text.indexof ("%e"); }} return result + text; }/** * Utf8url encoded to character * @param text * @return */private static final String Codetoword (string text) {string Result if (Utf8codecheck (text)) {byte[] code = new BYTE[3]; Code[0] = (byte) (Integer.parseint (text.substring (1, 3), 16)-256); CODE[1] = (byte) (Integer.parseint (Text.substring (4, 6), 16)-256); CODE[2] = (byte) (Integer.parseint (Text.substring (7, 9), 16)-256); try {result = new String (code, "UTF-8"); }catch (Unsupportedencodingexception ex) {result = null; }} else {result = text; } return result; }/** * Encoding is valid * @param text * @return */private static Final Boolean Utf8codecheck (string text) {string sig n = ""; if (Text.startswith ("%e")) for (int i = 0, p = 0; P! =-1; i++) {p = text.indexof ("%", p); if (P! =-1) p++; Sign + = P; } return Sign.equals ("147-1"); }/** * Determines if utf8url encoding * @param text * @return */public static final Boolean isutf8url (String text) {//each Chinese character will is encoded as a string with%e opening length of 9 text = Text.tolowercase (); int p = text.indexof ("%"); if (P! =-1 && text.length ()-p > 9) {text = Text.substring (p, p + 9); } return Utf8codecheck (text); /** * Test * @param args * @throws unsupportedencodingexception */@SuppressWarnings ("deprecation") public static void Main (string[] args) throws Unsupportedencodingexception {//system.out.println (Chartools.utf8urlencode ("I-it-stuffy")); String URL; String url2 = "https://www.baidu.com/s?wd= language? &rsv_spt=1&rsv_iqid=0Xd13fd9040001fb1d&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter =1&rsv_sug3=2&rsv_sug1=2&rsv_sug7=101&rsv_sug2=0&inputt=774&rsv_sug4=1367 &AAAA=1 "; url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91 %e5%9c%a8%e7%ba%bf%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2&btng=%e6%90%9c%e7%b4%a2&lr= "; String Utf8urlencode = Utf8urlencode ("https://www.baidu.com?wd= language &a=3%?:"); String url = "http://www.google.com/search?hl=zh-cn&newwindow=1&q= China Encyclopedia Online Full-text search &btng= searching &lr="; if (Chartoolsutil.isutf8url (URL2)) {String Ldecode = Chartoolsutil.utf8urldecode (URL2); System.out.println (Ldecode); String lencode1 = Chartoolsutil.utf8urlencode (Ldecode); String Lencode2 = Urlencoder.encode (Ldecode, "UTF-8"); System.out.println ("Uft8:" +lencode1+ "\nurlencoder:" +lencode2); System.out.println (Chartools.utf8urldecode (URL)); }//url = "http://www.google.com/search?hl=zh-cn&newwindow=1&q= China Encyclopedia Online Full-text search &btng= searching &lr="; if (! Chartoolsutil.isutf8url (URL)) {System.out.println (Chartoolsutil.utf8urlencode (URL)); } }}
Http URL Chinese encoding and decoding tool class