1. 程式人生 > >cookie存中文報錯問題

cookie存中文報錯問題

1.問題
cookie中存中文報錯,比如說:
Cookie cookie=new Cookie("name","謝謝");
cookie.setMaxAge(3600);
response.addCookie(cookie);

就會報錯:control character in value,consider base 64 encoding……

2.解決辦法
如報錯所說對中文采用base64編碼
3.具體程式碼
1.建立cookie

String val=java.net.URLEncoder.encode("謝謝","UTF-8");
Cookie cookie=new
Cookie("name",val);

2.取cookie

String val=java.net.URLDecoder.decode("要解的內容","UTF-8");