1、使用URLEncoder进行编码
String encode = URLEncoder.encode(keyWord, "UTF-8");
2、使用 URLDecoder进行解码
String decode = URLDecoder.decode(encode, "UTF-8");
示例代码:
public static void jiema() throws UnsupportedEncodingException{
String keyWord = "你好中国!";
//使用URLEncoder进行编码
String encode = URLEncoder.encode(keyWord, "UTF-8");
//使用 URLDecoder进行解码
String decode = URLDecoder.decode(encode, "UTF-8");
System.out.println("编码之后的内容:"+encode);
System.out.println("解码之后的内容:"+decode);
}