紀(jì)念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/中國(guó)測(cè)試
               *
               * @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/中國(guó)測(cè)試
               *
               * @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/中國(guó)測(cè)試?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 閱讀(2007) 評(píng)論(0)  編輯  收藏


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


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

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 苍溪县| 西贡区| 惠东县| 怀集县| 金昌市| 德昌县| 乐安县| 舞钢市| 久治县| 土默特右旗| 视频| 嵊州市| 河源市| 邮箱| 中超| 吉木乃县| 铁岭县| 汝城县| 电白县| 咸丰县| 舒城县| 大足县| 嘉善县| 谷城县| 扎兰屯市| 博爱县| 宁城县| 克东县| 宜君县| 台前县| 左云县| 河西区| 望城县| 准格尔旗| 罗江县| 隆尧县| 南漳县| 清远市| 格尔木市| 仪征市| 南安市|