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);
}
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);
}
本文介绍了一种处理URI中中文字符的编码方法。通过遍历字符串中的每一个字符,判断是否为中文字符,如果是则使用URLEncoder进行UTF-8编码。此方法适用于需要将包含中文的URL进行转义处理的场景。
1404

被折叠的 条评论
为什么被折叠?



