http://tech.ddvip.com/2009-06/1244637495123230.html
java代码如下(原帖代码在ucs2代码忘记了斜杠\,此处代码为自己重新抄写的)
package
com.ie;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* 正则替换字符串里面的汉字部分。
*
* @author 赵学庆 www.java2000.net
*/
public class URLEncoderZH {
public static void main(String[] args) throws Exception {
String str = " http//192.168.1.1:8080/resources/电话.xls " ;
System.out.println(encode(str, " utf-8 " ));
}
private static String zhPattern = " [\u4e00-\u9fa5]+ " ;
/**
* 替换字符串
*
* @param str 被替换的字符串
* @param charset 字符集
* @return 替换好的
* @throws UnsupportedEncodingException 不支持的字符集
*/
public static String encode(String str, String charset)
throws UnsupportedEncodingException {
Pattern p = Pattern.compile(zhPattern);
Matcher m = p.matcher(str);
StringBuffer b = new StringBuffer();
while (m.find()) {
m.appendReplacement(b, URLEncoder.encode(m.group( 0 ), charset));
}
m.appendTail(b);
return b.toString();
}
}
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* 正则替换字符串里面的汉字部分。
*
* @author 赵学庆 www.java2000.net
*/
public class URLEncoderZH {
public static void main(String[] args) throws Exception {
String str = " http//192.168.1.1:8080/resources/电话.xls " ;
System.out.println(encode(str, " utf-8 " ));
}
private static String zhPattern = " [\u4e00-\u9fa5]+ " ;
/**
* 替换字符串
*
* @param str 被替换的字符串
* @param charset 字符集
* @return 替换好的
* @throws UnsupportedEncodingException 不支持的字符集
*/
public static String encode(String str, String charset)
throws UnsupportedEncodingException {
Pattern p = Pattern.compile(zhPattern);
Matcher m = p.matcher(str);
StringBuffer b = new StringBuffer();
while (m.find()) {
m.appendReplacement(b, URLEncoder.encode(m.group( 0 ), charset));
}
m.appendTail(b);
return b.toString();
}
}