URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串;
URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将普通字符串转换成application/x-www-form-urlencoded MIME字符串。
String test= "{订单:2020.3.19}";
//采用utf-8字符集
System.out.println(URLEncoder.encode(test, "UTF-8"));
//采用GBK字符集
System.out.println(URLEncoder.encode(test, "GBK"));
//将application/x-www-form-urlencoded字符串转换成普通字符串
// 采用UTF-8字符集进行解码
System.out.println(URLDecoder.decode("%7B%E8%AE%A2%E5%8D%95%EF%BC%9A2020.3.19%7D", "UTF-8"));
// 采用GBK字符集进行解码
System.out.println(URLDecoder.decode("%7B%B6%A9%B5%A5%A3%BA2020.3.19%7D", "GBK"));
输出结果
%7B%E8%AE%A2%E5%8D%95%EF%BC%9A2020.3.19%7D
%7B%B6%A9%B5%A5%A3%BA2020.3.19%7D
{订单:2020.3.19}
{订单:2020.3.19}