<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    評論

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

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


      回復  更多評論   


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


    網站導航:
     
    主站蜘蛛池模板: 四虎必出精品亚洲高清| 两个人看www免费视频| 亚洲福利视频一区二区| 三年片在线观看免费观看大全一| 亚洲乱人伦精品图片| 亚洲国产成人VA在线观看| 久久青草精品38国产免费| 亚洲色无码专区一区| 亚洲成AV人片在线观看ww| 成人无遮挡毛片免费看| 日韩电影免费在线观看网站| 亚洲自国产拍揄拍| 亚洲性猛交XXXX| 好爽又高潮了毛片免费下载| 免费av片在线观看网站| 精品久久久久久亚洲中文字幕| 亚洲精品无码不卡| 亚洲毛片不卡av在线播放一区| 999国内精品永久免费视频| japanese色国产在线看免费| 日韩亚洲不卡在线视频中文字幕在线观看 | 一级特黄a大片免费| 久久亚洲精品成人av无码网站| 国产又黄又爽又猛的免费视频播放| 99久久99久久精品免费观看| 国产精品亚洲天堂| 亚洲成a人片77777群色| 亚洲宅男天堂在线观看无病毒| 精品国产免费观看久久久 | 午夜国产精品免费观看| 97超高清在线观看免费视频| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲人成电影在线观看网| 亚洲综合色婷婷七月丁香| 国产中文字幕免费| 野花高清在线电影观看免费视频 | 一级毛片**不卡免费播| 一级一黄在线观看视频免费| 亚洲av午夜国产精品无码中文字| 亚洲欧洲视频在线观看| 亚洲AV永久无码精品成人|