當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          很簡單,程序如下:
          class Test{
              
          public static void main(String[] args) throws Exception{
                  
          // 漢字變成UFT8編碼
                  System.out.println(URLEncoder.encode("何楊""utf-8"));
                  
                  
          // 將UFT8編碼的文字還原成漢字
                  System.out.println(URLDecoder.decode("%E4%BD%95%E6%9D%A8""utf-8"));        
              }
          }

          輸出如下:
          %E4%BD%95%E6%9D%A8
          何楊

          下面是一個輔助的工具類:
          import java.io.UnsupportedEncodingException;
          import java.net.URLDecoder;
          import java.net.URLEncoder;


          /**
           * UTF8轉碼器
           * 
          @author heyang
           *
           
          */
          public class UTF8Coder{
              
          private static final String UTF_8 = "utf-8";// 編碼形式

              
          /**
               * 對文字進行UTF8轉碼
               * 
          @param str
               * 
          @return
               
          */
              
          public static String encode(String str){
                  
          try {
                      
          return URLEncoder.encode(str, UTF_8);
                  } 
          catch (UnsupportedEncodingException e) {
                      
          return null;
                  }
              }
              
              
          /**
               * 將轉碼后的文字還原
               * 
          @param str
               * 
          @return
               
          */
              
          public static String decode(String str){
                  
          try {
                      
          return URLDecoder.decode(str, UTF_8);
                  } 
          catch (UnsupportedEncodingException e) {
                      
          return null;
                  }
              }
          }


          posted on 2010-12-11 12:06 何楊 閱讀(2102) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 遂宁市| 和静县| 石家庄市| 华池县| 临湘市| 中卫市| 开平市| 伊吾县| 平江县| 通化市| 施甸县| 柏乡县| 巨鹿县| 游戏| 南昌县| 潼关县| 恩平市| 得荣县| 宜良县| 博野县| 辽宁省| 无棣县| 榆树市| 威海市| 北碚区| 从江县| 扶沟县| 灵宝市| 长宁区| 区。| 喜德县| 蛟河市| 黄冈市| 斗六市| 灵山县| 高台县| 攀枝花市| 吴旗县| 根河市| 三穗县| 桑日县|