紀念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++) {
               // 當(dāng)前字符是不是數(shù)字,或字母,特殊字符'\\', '/', ':'
               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 閱讀(2005) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

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

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临清市| 大兴区| 柘荣县| 北安市| 和林格尔县| 白河县| 万山特区| 凉山| 乐至县| 济源市| 鸡西市| 甘谷县| 兴业县| 迁安市| 沙雅县| 福鼎市| 岳普湖县| 公安县| 西宁市| 天长市| 姚安县| 介休市| 光泽县| 鹤庆县| 西安市| 巴彦淖尔市| 航空| 内黄县| 涪陵区| 威远县| 乌鲁木齐市| 台州市| 东阿县| 广州市| 清丰县| 平舆县| 德兴市| 天镇县| 东光县| 马关县| 汨罗市|