JAVA流通桥

JAVA启发者

统计

留言簿(3)

AJAX相关网址

Eclipse相关网址

Hibernate

java相关网址

LINUX相关网址

webwork相关网址

友好链接

阅读排行榜

评论排行榜

url转换工具

  1import java.io.UnsupportedEncodingException;
  2import java.net.URLDecoder;
  3
  4public class CharTools {
  5
  6    /**
  7     * 转换编码 ISO-8859-1到GB2312
  8     * 
  9     * @param text
 10     * @return
 11     */

 12    public String ISO2GB(String text) {
 13        String result = "";
 14        try {
 15            result = new String(text.getBytes("ISO-8859-1"), "GB2312");
 16        }
 catch (UnsupportedEncodingException ex) {
 17            result = ex.toString();
 18        }

 19        return result;
 20    }

 21
 22    /**
 23     * 转换编码 GB2312到ISO-8859-1
 24     * 
 25     * @param text
 26     * @return
 27     */

 28    public String GB2ISO(String text) {
 29        String result = "";
 30        try {
 31            result = new String(text.getBytes("GB2312"), "ISO-8859-1");
 32        }
 catch (UnsupportedEncodingException ex) {
 33            ex.printStackTrace();
 34        }

 35        return result;
 36    }

 37
 38    /**
 39     * Utf8URL编码
 40     * 
 41     * @param s
 42     * @return
 43     */

 44    public String Utf8URLencode(String text) {
 45        StringBuffer result = new StringBuffer();
 46
 47        for (int i = 0; i < text.length(); i++{
 48
 49            char c = text.charAt(i);
 50            if (c >= 0 && c <= 255{
 51                result.append(c);
 52            }
 else {
 53
 54                byte[] b = new byte[0];
 55                try {
 56                    b = Character.toString(c).getBytes("UTF-8");
 57                }
 catch (Exception ex) {
 58                }

 59
 60                for (int j = 0; j < b.length; j++{
 61                    int k = b[j];
 62                    if (k < 0)
 63                        k += 256;
 64                    result.append("%" + Integer.toHexString(k).toUpperCase());
 65                }

 66
 67            }

 68        }

 69
 70        return result.toString();
 71    }

 72
 73    /**
 74     * Utf8URL解码
 75     * 
 76     * @param text
 77     * @return
 78     */

 79    public String Utf8URLdecode(String text) {
 80        String result = "";
 81        int p = 0;
 82
 83        if (text != null && text.length() > 0{
 84            text = text.toLowerCase();
 85            p = text.indexOf("%e");
 86            if (p == -1)
 87                return text;
 88
 89            while (p != -1{
 90                result += text.substring(0, p);
 91                text = text.substring(p, text.length());
 92                if (text == "" || text.length() < 9)
 93                    return result;
 94
 95                result += CodeToWord(text.substring(09));
 96                text = text.substring(9, text.length());
 97                p = text.indexOf("%e");
 98            }

 99
100        }

101
102        return result + text;
103    }

104
105    /**
106     * utf8URL编码转字符
107     * 
108     * @param text
109     * @return
110     */

111    private String CodeToWord(String text) {
112        String result;
113
114        if (Utf8codeCheck(text)) {
115            byte[] code = new byte[3];
116            code[0= (byte) (Integer.parseInt(text.substring(13), 16- 256);
117            code[1= (byte) (Integer.parseInt(text.substring(46), 16- 256);
118            code[2= (byte) (Integer.parseInt(text.substring(79), 16- 256);
119            try {
120                result = new String(code, "UTF-8");
121            }
 catch (UnsupportedEncodingException ex) {
122                result = null;
123            }

124        }
 else {
125            result = text;
126        }

127
128        return result;
129    }

130
131    /**
132     * 编码是否有效
133     * 
134     * @param text
135     * @return
136     */

137    private boolean Utf8codeCheck(String text) {
138        String sign = "";
139        if (text.startsWith("%e"))
140            for (int i = 0, p = 0; p != -1; i++{
141                p = text.indexOf("%", p);
142                if (p != -1)
143                    p++;
144                sign += p;
145            }

146        return sign.equals("147-1");
147    }

148
149    /**
150     * 是否Utf8Url编码
151     * 
152     * @param text
153     * @return
154     */

155    public boolean isUtf8Url(String text) {
156        text = text.toLowerCase();
157        int p = text.indexOf("%");
158        if (p != -1 && text.length() - p > 9{
159            text = text.substring(p, p + 9);
160        }

161        return Utf8codeCheck(text);
162    }

163
164    /**
165     * 测试
166     * 
167     * @param args
168     */

169    public static void main(String[] args) {
170
171        CharTools charTools = new CharTools();
172
173        String url;
174
175        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=";
176        String aa = "%d7%e9%d6%af";
177        if (charTools.isUtf8Url(url)) {
178            System.out.println(charTools.Utf8URLdecode(url));
179        }
 else {
180            System.out.println(URLDecoder.decode(url));
181        }

182        if (charTools.isUtf8Url(aa)) {
183        }
 else {
184            System.out.println(URLDecoder.decode(aa));
185        }

186
187        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";
188        if (charTools.isUtf8Url(url)) {
189            System.out.println(charTools.Utf8URLdecode(url));
190        }
 else {
191            System.out.println(URLDecoder.decode(url));
192        }

193
194    }

195
196}

197
使用自然会明白。

posted on 2007-08-08 16:56 朱岩 阅读(1837) 评论(1)  编辑  收藏 所属分类: 中文乱码问题

评论

# re: url转换工具 2012-06-19 11:06 阿斯顿

8llfff8888x8f8s888-s5d44fv45cvv1v11455---sd454vv116546664d5d64vv12vv1v2v22vd1fv1d23d132d23d2dv123dd5d4d5d4d5d4564564564556455556s456%%%^dsd12d312313223123


  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: