紀念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 閱讀(2008) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 张家口市| 武冈市| 兰西县| 册亨县| 泸水县| 公主岭市| 宁夏| 曲阳县| 江山市| 宁都县| 贡觉县| 锦屏县| 游戏| 合水县| 绿春县| 平凉市| 宣威市| 旬阳县| 新丰县| 福鼎市| 菏泽市| 平谷区| 金堂县| 木兰县| 大同市| 金昌市| 新野县| 枣强县| 信宜市| 奇台县| 大田县| 九龙城区| 嘉善县| 中宁县| 抚松县| 大余县| 修武县| 新泰市| 石渠县| 绵竹市| 贵阳市|