解决中文乱码问题
2016-07-20 12:44
302 查看
1、使用带有缓冲区的字符流读文件产生的乱码
解决方案:让输入流读取文件时按照文件的编码格式来度
InputStream is=new FileInputStream("text.txt");
Reader reader=new InputStreamReader(is,"UTF-8");
BufferedReader br=new BufferedReader(reader);
2、post方式提交时产生的乱码
1>设置请求的编码方法:
request.setCharacterEncoding("utf-8");
2>设置响应的编码方法:
response.setCharacterEncoding("utf-8");
3、get方式提交时产生的乱码
1>治标:在读取数据时对数据进行编码
new String(username.getBytes("iso-8859-1"),"utf-8");
2>治本:通过配置tomacat\conf\server.xml文件
在<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" 添加:URIEncoding="utf-8" />
或者:添加useBodyEncodingForURI="true"
4、cookie填写中文用户名产生的乱码
4-1:先在提交时转码:Cookie cookie=new Cookie("user",URLEncoder.encode(username, "UTF-8"));
4-2:在注销时解码:String temp=URLDecoder.decode(cookies[i].getValue(),"UTF-8");
解决方案:让输入流读取文件时按照文件的编码格式来度
InputStream is=new FileInputStream("text.txt");
Reader reader=new InputStreamReader(is,"UTF-8");
BufferedReader br=new BufferedReader(reader);
2、post方式提交时产生的乱码
1>设置请求的编码方法:
request.setCharacterEncoding("utf-8");
2>设置响应的编码方法:
response.setCharacterEncoding("utf-8");
3、get方式提交时产生的乱码
1>治标:在读取数据时对数据进行编码
new String(username.getBytes("iso-8859-1"),"utf-8");
2>治本:通过配置tomacat\conf\server.xml文件
在<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" 添加:URIEncoding="utf-8" />
或者:添加useBodyEncodingForURI="true"
4、cookie填写中文用户名产生的乱码
4-1:先在提交时转码:Cookie cookie=new Cookie("user",URLEncoder.encode(username, "UTF-8"));
4-2:在注销时解码:String temp=URLDecoder.decode(cookies[i].getValue(),"UTF-8");
相关文章推荐
- CF698A. Vacations【DP】
- 个人学习-java-事
- 生活中的常识(二)
- Problem - D - Codeforces Fix a Tree
- jquery性能
- MFC学习笔记——解决:不能添加控件变量
- JavaScript random方法得到随机整数
- iOS核心动画-UIView封装动画
- hdu5723(16多校第1场,树上两点平均距离的期望)
- 《圣经》故事与典故
- 在VUX中使用自定义组件要注意scroller组件等vux组件的覆盖
- 陪审团制度
- python基础之 Python os._exit() sys.exit() exit()区别
- CSS样式自动换行(强制换行)与强制不换行
- httpoxy 漏洞预警及修复方案
- 大整数类模板
- 负载均衡-会话保持,session同步(转载)
- uva 401 回文词
- 在SQLserver中stuff 函数用法
- 可变參函数设计