import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
public class URLDecodeExample {
public static void main(String[] args) {
try {
// 需要解码的URL字符串
String encodedUrl = "https://example.com/path?name=John%20Doe&city=New%20York";
// 使用URLDecoder.decode方法进行解码
// 第二个参数指定了字符集,这里使用UTF-8
String decodedUrl = URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8.toString());
// 输出解码后的URL
System.out.println("Decoded URL: " + decodedUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.net.URLDecoder
用于解码URL字符串,java.nio.charset.StandardCharsets
用于指定字符集。encodedUrl
是一个包含编码字符(如 %20
)的URL。URLDecoder.decode
方法:该方法将编码的URL字符串解码为原始字符串。第二个参数指定了字符集,这里使用的是 UTF-8。System.out.println
输出解码后的URL。下一篇:java 时间转换
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站