今天在学习Cookie的时候,了解到Cookie不能直接存储中文。
如果要存储的话,需要进行转码:URL编码
对应的编码解码的方法依次是:
//URL编码
String value = "中国";
value = URLEncoder.encode(value, "utf-8");
//URL解码
String value = "中国";
value = URLDecoder.decode(value, "utf-8");
简单的测试下
public class test {
public static void main(String[] args) throws UnsupportedEncodingException {
String value = "中国";
value = URLEncoder.encode(value,"utf-8");
System.out.println("编码:"+value);
value = URLDecoder.decode(value,"utf-8");
System.out.println("解码:"+value);
}
}