从网上获取的 中文转码工具
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 {//对中文进行转码 ???????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 ??* 单个中文字符被转码为 以%E起始长度为9的字符串 ??*/ ?public static final 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编码转字符 ??* @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; ?} ?/** ??* 编码是否有效 ??* @param text ??* @return ??*/ ?private static final 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 static final boolean isUtf8Url(String text) { ?//每个中文汉字会被编码成 ?以%E开头长度为9的字符串 ?????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 * @throws UnsupportedEncodingException ???*/ ?@SuppressWarnings("deprecation")public static void main(String[] args) throws UnsupportedEncodingException { ??//System.out.println(CharTools.Utf8URLencode("我它闷"));String url;String url2 = "https://www.baidu.com/s?wd=语 文 ?&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=语文&a=3 % ? :"); ??????????// String url = "http://www.google.com/search?hl=zh-cn&newwindow=1&q=中国大百科在线全文检索&btng=搜索&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=中国大百科在线全文检索&btng=搜索&lr="; ???if(!CharToolsUtil.isUtf8Url(url)){ ???????System.out.println(CharToolsUtil.Utf8URLencode(url)); ???} ?}}
Http URL中文编码解码工具类
原文地址:http://www.cnblogs.com/northern-light/p/8021972.html