一、前言
通过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@*
Title:字符编码工具类
@b@*Description:
@b@*Copyright: Copyright (c) 2007
@b@*Company:
@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 = 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 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) {@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