一、前言
通过java.net.URLDecoder定义CharTools字符编码工具类,实现转换编码 ISO-8859-1到GB2312方法ISO2GB转换、转换编码 GB2312到ISO-8859-1方法GB2ISO转换、中文网址Utf8URL编码/解码Utf8URLencode/Utf8URLdecode、判断是否Utf8Url编码isUtf8Url等方法代码示例。
二、代码示例
package com.xwood.util;@b@@b@import java.io.UnsupportedEncodingException;@b@import java.net.URLDecoder;@b@@b@/**@b@* <p>Title:字符编码工具类 </p>@b@* <p>Description: </p>@b@* <p>Copyright: Copyright (c) 2007</p>@b@* <p>Company: </p>@b@* @version 1.0@b@*/@b@@b@public class CharTools{@b@ @b@ /**@b@ * 转换编码 ISO-8859-1到GB2312@b@ * @param text@b@ * @return@b@ */@b@ public static final String ISO2GB(String text){@b@ String result = "";@b@ try {@b@ result = new String(text.getBytes("ISO-8859-1"), "GB2312");@b@ }catch (UnsupportedEncodingException ex) {@b@ result = ex.toString();@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * 转换编码 GB2312到ISO-8859-1@b@ * @param text@b@ * @return@b@ */@b@ public static final String GB2ISO(String text) {@b@ String result = "";@b@ try {@b@ result = new String(text.getBytes("GB2312"), "ISO-8859-1");@b@ }catch (UnsupportedEncodingException ex) {@b@ ex.printStackTrace();@b@ }@b@ return result;@b@ }@b@ @b@ /**@b@ * Utf8URL编码@b@ * @param s@b@ * @return@b@ */@b@ public static final String Utf8URLencode(String text) {@b@ StringBuffer result = new StringBuffer();@b@ for (int i = 0; i < text.length(); i++) {@b@ char c = text.charAt(i);@b@ if (c >= 0 && c <= 255) {@b@ result.append(c);@b@ }else {@b@@b@ byte[] b = new byte[0];@b@ try {@b@ b = Character.toString(c).getBytes("UTF-8");@b@ }catch (Exception ex) {@b@ }@b@@b@ for (int j = 0; j < b.length; j++) {@b@ int k = b[j];@b@ if (k < 0) k += 256;@b@ result.append("%" + Integer.toHexString(k).toUpperCase());@b@ }@b@ }@b@ }@b@ return result.toString();@b@ }@b@@b@ /**@b@ * Utf8URL解码@b@ * @param text@b@ * @return@b@ */@b@ public static final String Utf8URLdecode(String text) {@b@ String result = "";@b@ int p = 0;@b@@b@ if (text!=null && text.length()>0){@b@ text = text.toLowerCase();@b@ p = text.indexOf("%e");@b@ if (p == -1) return text;@b@@b@ while (p != -1) {@b@ result += text.substring(0, p);@b@ text = text.substring(p, text.length());@b@ if (text == "" || text.length() < 9) return result;@b@@b@ result += CodeToWord(text.substring(0, 9));@b@ text = text.substring(9, text.length());@b@ p = text.indexOf("%e");@b@ }@b@ }@b@ return result + text;@b@ }@b@@b@ /**@b@ * utf8URL编码转字符@b@ * @param text@b@ * @return@b@ */@b@ private static final String CodeToWord(String text) {@b@ String result;@b@ if(Utf8codeCheck(text)) {@b@ byte[] code = new byte[3];@b@ code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);@b@ code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);@b@ code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);@b@ try {@b@ result = new String(code, "UTF-8");@b@ }catch (UnsupportedEncodingException ex) {@b@ result = null;@b@ }@b@ }else {@b@ result = text;@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * 编码是否有效@b@ * @param text@b@ * @return@b@ */@b@ private static final boolean Utf8codeCheck(String text){@b@ String sign = "";@b@ if (text.startsWith("%e"))@b@ for (int i = 0, p = 0; p != -1; i++) {@b@ p = text.indexOf("%", p);@b@ if (p != -1)@b@ p++;@b@ sign += p;@b@ }@b@ return sign.equals("147-1");@b@ }@b@@b@ @b@ /**@b@ * 判断是否Utf8Url编码@b@ * @param text@b@ * @return@b@ */@b@ public static final boolean isUtf8Url(String text) {@b@ text = text.toLowerCase();@b@ int p = text.indexOf("%");@b@ if (p != -1 && text.length() - p > 9) {@b@ text = text.substring(p, p + 9);@b@ }@b@ return Utf8codeCheck(text);@b@ }@b@@b@ /**@b@ * 测试@b@ */@b@ public static void main(String[] args) {@b@ //CharTools charTools = new CharTools();@b@ String 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=";@b@ if(CharTools.isUtf8Url(url)){@b@ System.out.println(CharTools.Utf8URLdecode(url));@b@ }else{@b@ System.out.println(URLDecoder.decode(url));@b@ }@b@@b@ 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";@b@ if(CharTools.isUtf8Url(url)){@b@ System.out.println(CharTools.Utf8URLdecode(url));@b@ }else{@b@ System.out.println(URLDecoder.decode(url));@b@ }@b@ @b@ }@b@ @b@ @b@}
控制台打印结果
http://www.google.com/search?hl=zh-cn&newwindow=1&q=中国大百科在线全文检索&btng=搜索&lr=@b@http://www.baidu.com/baidu?word=中国大百科在线全文检索&tn=myie2dg