Java URLEncoder的作用就是在URL网址携带参数时,不会因为特殊字符或编码导致参数无法正确传递到,经过URLEncoder编码之后,使用端需要用URLDecoder类来解码URL编码信息,下面是Java URLEncoder与URLDecoder编码解码的使用示例,可以作为参考,非常的简单!package test;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class UrlDemo {
public static void main(String[] args) {
String ss = "我是中国人";
//URL编码
String encodeStr = encodeValue(ss);
System.out.println(encodeStr);
//URL解码
String decodeStr = decodeValue(encodeStr);
System.out.println(decodeStr);
}
//URLdecoder编码
public static String decodeValue(String value) {
try {
return URLDecoder.decode(value, "utf-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
//URLEncoder编码
private static String encodeValue(String value) {
try {
return URLEncoder.encode(value, "utf-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
}
上面的URL编码解码方法输出的结果如下所示:%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA
我是中国人
来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/java/997.html