java中对url地址中的中文进行编码

    // URL中汉字编码
    private String urlEncodeChinese(String url) {
        try {
            Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
            String tmp = "";
            while (matcher.find()) {
                tmp = matcher.group();
                url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url.replace(" ", "%20");
    }

  

posted on 2021-07-30 17:22  itprobie-菜鸟程序员  阅读(1071)  评论(0编辑  收藏  举报