java: URLEncoder.encode only transcodes Chinese

Original: Java only converts Chinese characters to URLEncoder

Cause of transcoding: The display of online pictures is abnormal, because IOS cannot display the picture links with Chinese characters. In the previous article, we introduced the normal display through transcoding. Later, we found that there was a problem with the original transcoding, not only for Chinese. Transcoding, "%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE" was also transcoded twice, causing the link to fail.

    @Test
    public void test() throws UnsupportedEncodingException {
    
    
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");

        String a = "https://kos-oss-test.oss-cn-hangzhou.aliyuncs.com/images/企业微信截图_20211229e0a4899a905a4b6d8f0f2d88f8288cd0%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_16407530994802.png";
        Matcher m = p.matcher(a);
        StringBuffer b = new StringBuffer();
        while (m.find()) {
    
    
            m.appendReplacement(b, URLEncoder.encode(m.group(0), "utf-8"));
        }
        m.appendTail(b);
        System.out.println(b.toString());
        String encode = URLEncoder.encode("企业微信截图", "utf-8");
        System.out.println(encode);
    }

result:

https://kos-oss-test.oss-cn-hangzhou.aliyuncs.com/images/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20211229e0a4899a905a4b6d8f0f2d88f8288cd0%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_16407530994802.png
%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE

By comparison, it is true that only the Chinese part is transcoded

Guess you like

Origin blog.csdn.net/RoyRaoHR/article/details/122218955