A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© cunbai 中级黑马   /  2015-5-24 15:19  /  286 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package log;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则替换字符串里面的汉字部分。
*
* @author 赵学庆 www.java2000.net
*/
public class URLEncoderHZ {
  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();
  }
}

运行结果
http://192.168.1.1:8080/resources/%E7%94%B5%E8%AF%9D.xls

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马