用java把google和baidu的URL編碼還原成明文字串 出自:http://www.flashman.com.cn/blog/ ... d=27&log_id=720 作者:flashman 因?yàn)樵谧鲦溄觼碓唇y(tǒng)計(jì)的時(shí)候需要把 http://www.baidu.com/baidu?word= ... B%F7&tn=myie2dg 這類的URL編碼還原成明文字串,一般大部分的網(wǎng)站都是用普通的URL編碼形式,如上面鏈接中的badu,這種很容易轉(zhuǎn)換和還原,Java包里提供了兩個(gè)類的不同方法URLEncode.encode()和URLDecode.decode()可以很方便實(shí)現(xiàn),但也有特別一點(diǎn)的就是Google了,http://www.google.com/search?hl= ... 9C%E7%B4%A2&lr= 他們的編碼和別人不一樣,如果使用URLDecode.decode()的話則變成亂碼,查詢的一些相關(guān)資料都說Google使用的是UTF-8編碼,這點(diǎn)我就有些奇怪了,如果Google使用的是UTF-8編碼,那別人使用的又是什么?IE的高級選項(xiàng)里不是有項(xiàng)“始終以UTF-8形式發(fā)送URL”的嗎?但是UTF-8一個(gè)中文是3byte,而一般的編碼則是2個(gè)byte,這就是為什么一般的URL中是以兩組‘%‘代碼表示一個(gè)漢字,如“中”的URL編碼為"%D6%D0",而UTF-8則為3組,“中”為"%E4%B8%AD",這個(gè)問題我在Google里也沒得到較好回答。我對各種編碼形式了解的不是很好,之前只看過如何將字符串轉(zhuǎn)成Utf8-URL編碼的方法,其實(shí)也挺簡單的,直接轉(zhuǎn)成byte后直接取其16進(jìn)制值前面加個(gè)%就行,還原方法在網(wǎng)上搜了幾圈居然沒發(fā)現(xiàn)有現(xiàn)成的!倒是也是幾個(gè)人在CSDN問了此類的問題。最后還是決定自己搞定了,基本上是toUTF8的原路退回法,再加了個(gè)檢測URL鏈接是否UTF-8形式的方法,覺得已經(jīng)蠻好用了??梢阅贸鰜韘hare一下。 import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.net.URLDecoder; /** * <p>Title:字符編碼工具類 </p> * <p>Description: </p> * <p>Copyright: flashman.com.cn Copyright (c) 2005</p> * <p>Company: flashman.com.cn </p> * @author: jeffzhu * @version 1.0 */ public class CharTools { /** * 轉(zhuǎn)換編碼 ISO-8859-1到GB2312 * @param text * @return */ public String ISO2GB(String text) { String result = ""; try { result = new String(text.getBytes("ISO-8859-1"), "GB2312"); } catch (UnsupportedEncodingException ex) { result = ex.toString(); } return result; } /** * 轉(zhuǎn)換編碼 GB2312到ISO-8859-1 * @param text * @return */ public String GB2ISO(String text) { String result = ""; try { result = new String(text.getBytes("GB2312"), "ISO-8859-1"); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } return result; } /** * Utf8URL編碼 * @param s * @return */ public 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 { 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解碼 * @param text * @return */ public String Utf8URLdecode(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編碼轉(zhuǎn)字符 * @param text * @return */ private 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; } /** * 編碼是否有效 * @param text * @return */ private boolean Utf8codeCheck(String text){ String sign = ""; 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"); } /** * 是否Utf8Url編碼 * @param text * @return */ public boolean isUtf8Url(String text) { text = text.toLowerCase(); int p = text.indexOf("%"); if (p != -1 && text.length() - p > 9) { text = text.substring(p, p + 9); } return Utf8codeCheck(text); } /** * 測試 * @param args */ public static void main(String[] args) { CharTools charTools = new CharTools(); String url; 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="; if(charTools.isUtf8Url(url)){ System.out.println(charTools.Utf8URLdecode(url)); }else{ System.out.println(URLDecoder.decode(url)); } url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg"; if(charTools.isUtf8Url(url)){ System.out.println(charTools.Utf8URLdecode(url)); }else{ System.out.println(URLDecoder.decode(url)); } } } |