當(dāng)柳上原的風(fēng)吹向天際的時候...

          真正的快樂來源于創(chuàng)造

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            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轉(zhuǎn)碼器
           * 
          @author heyang
           *
           
          */
          public class UTF8Coder{
              
          private static final String UTF_8 = "utf-8";// 編碼形式

              
          /**
               * 對文字進行UTF8轉(zhuǎn)碼
               * 
          @param str
               * 
          @return
               
          */
              
          public static String encode(String str){
                  
          try {
                      
          return URLEncoder.encode(str, UTF_8);
                  } 
          catch (UnsupportedEncodingException e) {
                      
          return null;
                  }
              }
              
              
          /**
               * 將轉(zhuǎn)碼后的文字還原
               * 
          @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 何楊 閱讀(2101) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新巴尔虎右旗| 宜都市| 申扎县| 迁西县| 红原县| 都兰县| 双柏县| 蒙城县| 宜章县| 邮箱| 沅陵县| 山阳县| 吕梁市| 鄱阳县| 揭东县| 周口市| 西盟| 肥乡县| 辛集市| 右玉县| 普洱| 当雄县| 青岛市| 五台县| 鸡西市| 沙湾县| 玉龙| 中阳县| 舟山市| 青冈县| 隆尧县| 毕节市| 金山区| 安化县| 油尖旺区| 洛宁县| 壤塘县| 亚东县| 精河县| 大连市| 中超|