紀念SUN

          Powered By Andy

          URL編碼

          package cn.struts.util;

          import java.util.Arrays;

          /**
           * 字符編碼
           */
          public class URLEnCodeing {

              private static final char[] c = { '\\', '/', ':', '?', '&'};

              /**
               * URL 編碼 http://localhost:8080/webproject/中國測試
               *
               * @param url http://localhost:8080/webproject/%4e2d%56fd%6d4b%8bd5
               *
               * @return
               */
              public static String escape(String url) {
           int length = url.length();
           StringBuilder sb = new StringBuilder(length);
           char currentChar;
           for (int i = 0; i < length; i++) {
               // 當前字符是不是數字,或字母,特殊字符'\\', '/', ':'
               currentChar = url.charAt(i);
               if(currentChar == 37)
            throw new RuntimeException("不能是%");
               if (currentChar <= 127) {
            sb.append(currentChar);
               } else {
            sb.append("%");
            sb.append(Integer.toString(currentChar, 16));
               }
           }

           return sb.toString();
              }

              /**
               * URL 編碼 http://localhost:8080/webproject/中國測試
               *
               * @param url http://localhost:8080/webproject/%4e2d%56fd%6d4b%8bd5?time=11234566&name=%4e16%754c
               *
               * @return
               */
              public static String unEscape(String url) {
           int length = url.length();
           StringBuilder sb = new StringBuilder(length);
           String[] str = url.split("%");
           sb.append(str[0]);
           // %4e2d //"8bd5?time=11234566&name="
           for (int i = 1; i < str.length; i++) {
               String s = str[i];
               if(s.length() > 4){
            sb.append((char)Integer.parseInt(s.substring(0, 4), 16));
            sb.append(s.substring(4));
               }else
               sb.append((char) Integer.parseInt(str[i], 16));
           }
           return sb.toString();
              }

              public static void main(String[] args) {
           String value = "http://localhost:8080/webpr&oject/中國測試?time=11234566&name=世界";
           String url = "http://localhost:8080/webp25r&oject/%4e2d%56fd%6d4b%8bd5?time=11234566&name=%4e16%754c";
           System.out.println(URLEnCodeing.escape(value));
           System.out.println(URLEnCodeing.unEscape(url));
           System.out.println(Integer.toString((char)'%',16));
           

              }

          }

          posted on 2010-02-22 18:57 Powered By Andy 閱讀(2004) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 绥德县| 大港区| 榕江县| 揭东县| 库车县| 巩留县| 凤山县| 南木林县| 买车| 共和县| 陆丰市| 将乐县| 土默特左旗| 定南县| 山阴县| 松滋市| 遂昌县| 康乐县| 阜新市| 西林县| 沁源县| 金门县| 渭源县| 宿迁市| 吉首市| 香格里拉县| 梨树县| 舞钢市| 红原县| 阳江市| 凯里市| 鄂托克前旗| 来安县| 海南省| 周口市| 洞头县| 广宗县| 榆中县| 公安县| 武汉市| 阳城县|