URLEncoder:将中文字符转换成url编码格式
@Test
public void testURLEncoder(){
String httpCode="[你好]";
try {
httpCode= URLEncoder.encode(httpCode,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(httpCode); //输出==>%5B%E4%BD%A0%E5%A5%BD%5D
}
URLDecoder:将url编码格式解码
@Test
public void testURLDecoder(){
//"[你好]"的url编码格式
String httpCode="%5B%E4%BD%A0%E5%A5%BD%5D";
try {
httpCode= URLDecoder.decode(httpCode,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(httpCode); //输出==>[你好]
}
StringUtils.replaceEach:局部替换字符
URL编码对照表可以参照这篇博客:https://blog.csdn.net/sinat_34439107/article/details/78242584
@Test
public void test(){
String[] urlCode=new String[]{"%5B","%5D"};
String[] trueCode=new String[]{"[","]"};
//"[你好]"的url编码格式
String httpCode="%5B%E4%BD%A0%E5%A5%BD%5D";
httpCode=StringUtils.replaceEach(httpCode,urlCode,trueCode);
System.out.println(httpCode); //输出==>[%E4%BD%A0%E5%A5%BD]
}