import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
public class URLDecoderExample {
public static void main(String[] args) {
String encodedString = "https://example.com/path?name=John%20Doe&city=New%20York";
try {
// 使用UTF-8字符集解码URL编码的字符串
String decodedString = URLDecoder.decode(encodedString, "UTF-8");
System.out.println("Decoded String: " + decodedString);
} catch (UnsupportedEncodingException e) {
// 如果指定的字符集不受支持,则抛出此异常
e.printStackTrace();
}
}
}
URLDecoder.decode
方法:该方法用于解码使用 application/x-www-form-urlencoded
MIME 格式的字符串。它将 %xx
形式的十六进制编码转换为原始字符。decode
方法时,需要指定字符集(例如 "UTF-8"
)。这确保了解码后的字符串与原始字符串一致。UnsupportedEncodingException
异常,因此需要进行捕获和处理。通过上述代码,你可以将一个经过 URL 编码的字符串解码为原始字符串。
上一篇:java的for循环
下一篇:java long 转 int
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站