将URL中的中文转为UTF-8编码

       
 1  /**
 2    * 将URL中的中文转为UTF-8编码
 3    * @param str
 4    * @return
 5    * @throws UnsupportedEncodingException
 6    */
 7   public String utfURL(String str) throws UnsupportedEncodingException{
 8   StringBuffer sb = new StringBuffer();
 9    int len = str.length();
10           for (int i = 0; i < len; i++) {
11               char c = str.charAt(i);
12                   if (c > 0x7F) {
13                   sb.append(URLEncoder.encode(String.valueOf(c),"utf-8"));
14                   }else {
15                   sb.append(c);
16                   }
17           }
18          return sb.toString();
19   }

 

posted @ 2012-07-08 02:24  jiang nan  阅读(1696)  评论(0编辑  收藏  举报