可以使用 java.net.URI.create() 方法来替代 java.net.URLEncoder.encode() 方法进行 URL 编码。URI.create() 方法可以将字符串转换为 URI 对象,并且自动地将字符串中的非法字符转义。示例如下:

String input = 'hello world';
URI uri = URI.create('http://example.com/path?query=' + input);
String encoded = uri.toASCIIString();
System.out.println(encoded); // http://example.com/path?query=hello%20world

在上面的示例中,我们使用 URI.create() 方法将输入字符串转换为 URI 对象,并将其附加到 URL 中作为查询参数。然后,我们调用 URI.toASCIIString() 方法来获取经过转义的字符串,以便在 URL 中使用。

Java URL 编码:替代 URLEncoder.encode() 的方法

原文地址: https://www.cveoy.top/t/topic/ogas 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录