`
andrew.yulong
  • 浏览: 166639 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

对url中的中文参数进行编码

    博客分类:
  • js
阅读更多

 

public static String urie(String str) {
  
if (StringUtils.isBlank(str)) {
   
return "";
  }
  String uri 
= "";
  
try {
   
for (int i=0;i<str.length();i++){
    String c
=str.substring(i,i+1);
      if(c.matches("[\u4e00-\u9fa5]")){

//  u4e00   u9fa5之间的代表是中文
     c=java.net.URLEncoder.encode(c, "UTF-8");
    }
    uri
=uri+c;
   }
  } 
catch (UnsupportedEncodingException e) {
   e.printStackTrace();
   
return "";
  }
  
return uri;
 }
 
 
public  static void main(String[] args){
     
    String a 
= urie("http://www.google.com?key=中国");
    System.out.print(a);
    } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics