解决ie下cookie中文乱码问题
2016-03-15 13:39
507 查看
今天在项目中碰到一个问题,chrome,Firefox浏览器下cookie中的中文都正常,唯独IE下出现乱码。
项目中全部采用utf-8编码,可是IE下现实仍然不正常。最终进过排查,发现IE传到后台的cookie值 ,
仍然采用ISO8859-1编码格式,所以导致了以utf-8解析的时候出现错误的情况。
![例如,这是IE浏览时cookie值:ÐìÖÝ (徐州的乱码形式)]
https://img-blog.csdn.net/20160315132528611
[这是chrome浏览时cookie值:å¾å· (徐州的乱码形式)]
(https://img-blog.csdn.net/20160315132909551)
这时候,对chrome和IE进行不同的解码就可以。
IE下:cookValue=new String(cookie.getValue().getBytes(“ISO8859-1”)); //这里结果直接得到中文
chrome下:cookValue=cookie.getValue();
项目中全部采用utf-8编码,可是IE下现实仍然不正常。最终进过排查,发现IE传到后台的cookie值 ,
仍然采用ISO8859-1编码格式,所以导致了以utf-8解析的时候出现错误的情况。
![例如,这是IE浏览时cookie值:ÐìÖÝ (徐州的乱码形式)]
https://img-blog.csdn.net/20160315132528611
[这是chrome浏览时cookie值:å¾å· (徐州的乱码形式)]
(https://img-blog.csdn.net/20160315132909551)
这时候,对chrome和IE进行不同的解码就可以。
IE下:cookValue=new String(cookie.getValue().getBytes(“ISO8859-1”)); //这里结果直接得到中文
chrome下:cookValue=cookie.getValue();
代码如下:
Cookie cookie = (Cookie) cookieMap.get(name); String cookValue = null; if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { // IE8,9浏览器 cookValue=new String(cookie.getValue().getBytes("ISO8859-1")); return cookValue; }else if (request.getHeader("User-Agent").toUpperCase().indexOf("TRIDENT") > 0) { // IE11浏览器 cookValue=new String(cookie.getValue().getBytes("ISO8859-1")); return cookValue; } else { // 其他浏览器 cookValue=cookie.getValue(); } cookValue = java.net.URLDecoder.decode(cookValue, "utf-8"); return utf8Togb2312(cookValue);//utf8Togb2312()是utf-8转中文函数,可以百度一个
相关文章推荐
- maven学习(下)利用Profile构建不同环境的部署包
- windowmanager params type
- CDC IsPrint
- spring介绍
- java实现斐波那契数列两种写法
- 抽象类和接口类
- Failed to connect to database. This release of the GeoDatabase is either invalid or out of date
- 加盐哈希
- linux sudo 命令
- linux下shell显示-bash-4.1#不显示路径解决方法
- 流域水文模拟
- 【数据结构】重新面对数据结构
- 关于.NET前后台提示框的那点事
- 如何扩展大规模Web网站的性能?
- [Songqw.Net 基础]WPF实现简单的插件化开发
- java set和get方法的使用
- leetcode 55. Jump Game
- asp.net mvc+angularjs+web api单页应用
- Darkstat:基于web的网络监控工具
- Kotlin:Android世界的Swift