Java中URLEncoderencode和encodeURIComponent的区别是什么举例说明
Java中的URLEncoder.encode和JavaScript中的encodeURIComponent都是对字符串进行URL编码的方法,但是它们之间有以下几点区别:
-
URLEncoder.encode是Java中的方法,而encodeURIComponent是JavaScript中的方法,二者使用的环境不同。
-
URLEncoder.encode默认编码方式是UTF-8,而encodeURIComponent默认编码方式是UTF-8。
-
URLEncoder.encode会将空格转换为加号+,而encodeURIComponent会将空格转换为%20。
例如,对于字符串"Hello World!",使用URLEncoder.encode和encodeURIComponent编码后的结果如下:
在Java中使用URLEncoder.encode编码:
String encoded = URLEncoder.encode("Hello World!", "UTF-8");
System.out.println(encoded);
结果为:"Hello+World%21"
在JavaScript中使用encodeURIComponent编码:
var encoded = encodeURIComponent("Hello World!");
console.log(encoded);
结果为:"Hello%20World%21
原文地址: https://www.cveoy.top/t/topic/fGct 著作权归作者所有。请勿转载和采集!