项目当中用到cookie保存中文,但是会报如下错误:
Control character in cookie value, consider BASE64 encoding your value
大概意思是保存到cookie当中的值存在控制字符,无法保存。但实际上数据是不存在这种问题的。再看后面的那句话,好像是将要保存的值进行了base64编码,可能是因为中文在编码时出现乱码导致一些控制字符的出现。
解决方案:将要保存的值进行URLEncoder.encode(value,"utf-8")编码。
在提取时,同样进行解码:
/**
* 添加cookie
* @param response
* @param name cookie的名称
* @param value cookie的值
* @param maxAge cookie存放的时间(以秒为单位,假如存放三天,即3*24*60*60; 如果值为0,cookie将随浏览器关闭而清除)
*/
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
try {
//注意此处的 URLEncoder
value=URLEncoder.encode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
if (maxAge>0) cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
/**
* 添加cookie
* @param response
* @param name cookie的名称
* @param value cookie的值
* @param maxAge cookie存放的时间(以秒为单位,假如存放三天,即3*24*60*60; 如果值为0,cookie将随浏览器关闭而清除)
* @param domain cookie的域名
* @param domain cookie的路径
*/
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge,String domain,String path) {
try {
value=URLEncoder.encode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Cookie cookie = new Cookie(name, value);
cookie.setDomain(domain);
cookie.setPath(path);
if (maxAge>0) cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
/**
* 获取cookie的值
* @param request
* @param name cookie的名称
* @return
*/
public static String getCookieByName(HttpServletRequest request, String name) {
Map<String, Cookie> cookieMap = readCookieMap(request);
if(cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
String value = null;
try {
value=URLDecoder.decode(cookie.getValue(),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}else{
return null;
}
}
protected static Map<String, Cookie> readCookieMap(HttpServletRequest request) {
Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (int i = 0; i < cookies.length; i++) {
cookieMap.put(cookies[i].getName(), cookies[i]);
}
}
return cookieMap;
}
分享到:
相关推荐
FBIK CharacterControl 1.2.2
s mbstring extension is enabled... (The automatic base64 encoding was added in the prior release, 1.4.8, and caused problems for users who don't control the receiving end of the requests.) Bug 7837.
由于原本把需要的数据存放到cookie中,但是cookie不能的key不能自动转译中文,出现乱码,后台服务器报错:Control character in cookie value or attribute.所以改为自定义map集合
unity3d控制插件 InControl 1.6.16
C#或者VB.Net 如果要调用JS,必不可少要用到Microsoft script control组件, 用过的人都知道有多方便, 但是官方版本只支持32位系统的, 64位系统使用会报错System.Runtime.InteropServices.COMException:“检索 ...
This book presents the reader, whether an electrical engineering student in power electronics or a design engineer, some typical power converter control problems and their basic digital solutions, ...
二进制到文本编码(base64_encode 和 base64_decode) 这是一个非常简单的项目,用于收集有关加密使用性能的更多信息。 如果数据库一直加密和解密。 我们可以在多大程度上妥协性能和其他问题。 目前,我没有使用...
Sliding Modes in Control and Optimization (英语) Vadim I. Utkin (作者) 高清电子版 The book is devoted to systems with discontinuous control. The study of discontinuous dynamic systems is a ...
unity 蓝牙手柄控制管理 InControl v1.8.0 最新版插件
包含InControl 1.6.16 ,InControl 1.7.0,InControl 1.7.2三个版本。根据自己unity版本选择合适的试用。如有需要请移步UnityAssetsStore支持正版:...
InControl 是 Unity 上一款统一的跨平台输入管理器,可以标准化常见控制器的映射。支持多种手柄 Xbox 360 控制器 • Xbox One 控制器 • PS3 和 PS4 控制器 • PlayStation Vita PSM • Apple MFi 控制器(iOS 7+、...
配套https://blog.csdn.net/ChaoChao66666/article/details/127361519?spm=1001.2014.3001.5501这篇博客的文件
unity InControl 1.6.16 相对来说是比较新的版本,大家可以试试
DameWare Mini Remote Control 远程控制 绿色版
Control and Simulation in LabVIEW
ControlCAN_x64.zip ControlCAN.h x64 ControlCAN.dll ControlCAN.lib ControlCAN_x32.zip ControlCAN.h x32 ControlCAN.dll ControlCAN.lib 接口函数库(二次开发库)使用说明书.pdf
Digital_Control_in_Power_Electronics,该书主要讲解电力电子元器件的数字化控制
Switching in systems and control
time con- trol approach is used in the voltage restoration which enables the voltages at all the DGs to converge to the reference value in finite time, and thus, the voltage and frequency control ...
网上下载的controlCAN库都是32位的,64位不能用,费了好大功夫才找到可以用64的库,里面也有32位