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 朱巖 閱讀(1867) 評論(1)  編輯  收藏 所屬分類: 中文亂碼問題

          評論

          # re: url轉換工具 2012-06-19 11:06 阿斯頓

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


            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 巴青县| 东港市| 华蓥市| 多伦县| 巴青县| 平泉县| 东平县| 沭阳县| 长治市| 昌乐县| 文山县| 子洲县| 宁蒗| 鹤岗市| 宿迁市| 瑞安市| 东辽县| 武夷山市| 长汀县| 威宁| 赫章县| 荣昌县| 海城市| 饶阳县| 台江县| 寿阳县| 徐闻县| 五寨县| 招远市| 礼泉县| 安达市| 营山县| 新疆| 碌曲县| 新建县| 高密市| 南丰县| 揭阳市| 沅陵县| 山阳县| 鲁甸县|