get请求中文乱码及get,post编码探究
在我使用get请求进行查询的时候遇到一个问题:
当我的请求参数中有中文时,出现乱码。
可是即使我设置了Spring的characterEncodingFilter,也还是出现乱码。
原因:tomcat默认使用ISO8859-1编码来解析get中的url参数,导致乱码。而characterEncodingFilter或者 request.setCharacterEncoding("UTF-8");都只针对post请求体有效。
下面对Http中get方法编码到tomcat的解码过程进行探究。
解决方法
- 更改tomcat中get方法默认ISO8859-1编码为utf-8编码。
找到conf/server.xml,在<Connector port="8082" protocol="HTTP/1.1"中加入URIEncoding="utf-8"。 - 将参数以iso8859-1编码转化为字节数组,然后再以UTF-8将字节数组转化为字符串。
userName = new String(userName.getBytes("ISO8859-1"), "UTF-8");
URL是怎么编码的?
参考 关于URL编码
一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。比如,世界上有英文字母的网址"http://www.abc.com",但是没有希腊字母的网址"http://www.aβγ.com"(读作阿尔法-贝塔-伽玛.com)。这是因为网络标准RFC 1738做了硬性规定。
这意味着,如果URL中有汉字,就必须编码后使用。但是麻烦的是,RFC 1738没有规定具体的编码方法,而是交给应用程序(浏览器)自己决定。这导致"URL编码"成为了一个混乱的领域。
不同的操作系统、不同的浏览器、不同的网页字符集,将导致完全不同的编码结果。经过测试,现在的浏览器大部分都是utf-8编码。但是为了兼容所有的浏览器,可以使用Javascript函数:encodeURI()。
encodeURI()是Javascript中真正用来对URL编码的函数。
它着眼于对整个URL进行编码,因此除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%。

它对应的解码函数是decodeURI()。

tomcat是怎么解码的?
get请求是使用url编码方式,而post请求基于请求体自身的编码。
推荐
- get请求含有url参数时,使用js自带的编码函数进行编码。
- post请求在content-type中设置charset=utf-8,否则使用页面默认编码。
get方法的编码
查看tomcat源码中,org.apache.catalina.connector.CoyoteAdapter的方法:
使用在conf/server.xml中 <Connector port="8082" protocol="HTTP/1.1">配置的URIEncoding作为将前端传过来的参数转化为字符数组的编码,缺省为ISO8859-1。
protected void convertURI(MessageBytes uri, Request request)
throws Exception {
ByteChunk bc = uri.getByteChunk();
int length = bc.getLength();
CharChunk cc = uri.getCharChunk();
cc.allocate(length, -1);
// 使用默认编码 ISO8859-1 将字节数组编程字符
String enc = connector.getURIEncoding();
if (enc != null) {
B2CConverter conv = request.getURIConverter();
try {
if (conv == null) {
conv = new B2CConverter(enc, true);
request.setURIConverter(conv);
} else {
conv.recycle();
}
} catch (IOException e) {
log.error("Invalid URI encoding; using HTTP default");
connector.setURIEncoding(null);
}
if (conv != null) {
try {
conv.convert(bc, cc, true);
uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength());
return;
} catch (IOException ioe) {
// Should never happen as B2CConverter should replace
// problematic characters
request.getResponse().sendError(
HttpServletResponse.SC_BAD_REQUEST);
}
}
}
// Default encoding: fast conversion for ISO-8859-1
byte[] bbuf = bc.getBuffer();
char[] cbuf = cc.getBuffer();
int start = bc.getStart();
for (int i = 0; i < length; i++) {
cbuf[i] = (char) (bbuf[i + start] & 0xff);
}
uri.setChars(cbuf, 0, length);
}
post方法的字符编码
如果在servlet的doPost方法中或者filter中设置了request的字符编码,那么就以设置的为准。
- request设置编码
public void doPost(HttpServletRequestrequest,HttpServletResponse response)
throws IOException,ServletException{
//必须在getParameter,getParameterNames,
//getParameterValues方法调用之前进行设置
request.setContentType("UTF-8");
}
- web.xml中配置filter
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
如果没有进行上面的配置,那么从http header中取出content-type,然后从content-type的值中取出charset的值,charset的值作为post的字符编码。
如content-type=application/x-www-form-urlencoded;charset=utf-8
那么,post的字符编码就是utf-8。
如果从http header中没有取到content-type中的charset,那么,就使用缺省的ISO-8859-1。
参考文档
get请求中文乱码及get,post编码探究的更多相关文章
- Spring-解决请求中文乱码问题
解决spring请求中文乱码问题 1.web.xml添加编码拦截器 <filter> <filter-name>CharacterEncoding</filter-nam ...
- 使用httpclient post请求中文乱码解决办法
使用httpclient post请求中文乱码解决办法 在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码 ...
- get请求与post请求中文乱码问题的解决办法
首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...
- 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢
目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...
- 解决Post请求中文乱码问题
解决Post请求中文乱码问题 req.setChracterEncoding()要在获取请求参数前调用才有效,不然还是乱码
- 5 Http请求中文乱码处理
java 乱码分很多种,这里主要研究解决http请求中出现乱码的情况. http请求出现中文乱码的主要原因:发送方与接收方编码不一致,服务器默认支持的编码与web应用不一致,如:tomcat 是国外程 ...
- SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?
在web.xml中 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-c ...
- Java中关于Servlet中请求中文乱码及文件下载
1,Servlet请求响应中文乱码问题 package com.demo.servlet; import java.io.PrintWriter; import java.io.IOException ...
- java web中get请求中文乱码在filter中解决
之前已经讲过get或者post方法的中文乱码问题,之前都是在每个方法中编写设置编码.如果程序变大,就会很繁琐,使用filter可以避免这种繁琐. 1)写一个encodingFilter进行编码设置 p ...
随机推荐
- 高通msm8909耳机调试
http://blog.csdn.net/mike8825/article/details/69489865?locationnum=3&fps=1 1.DTS相应修改: DTS相关代码:ke ...
- tty各种设备的情况
通常使用tty来简称各种类型的终端设备. (1)串口端口终端(/dev/ttySn) 串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备.计算机把每个串行端口都 ...
- LVS集群DR模式实例(4)
LVS集群DR模式实例 1. 实验拓扑图 2. 实验环境 3台CentOS6.4 64bit的服务器. 类型 IP DR eth0:10.20.73.20 VIP eth0:0 10.20.73.3 ...
- bzoj:1659: [Usaco2006 Mar]Lights Out 关灯
Description 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的少.他们知道按钮开关的位置,但喜闻乐见的是他们并没有手指.你得到 ...
- [bzoj1706] [usaco2007 Nov]relays 奶牛接力跑
大概是叫倍增Floyd? 显然最多200个点...f[i][j][k]表示从j到k,走2^i步的最小路程.就随便转移了.. 查询的话就是把n二进制位上是1的那些都并起来. #include<cs ...
- 并查集-HDU1232-畅通工程
转的其他人的.不知道谁的. 来看一个实例,杭电1232畅通工程 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的.最后要解决的是整幅图的连通性问题.比如随意 ...
- 2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分
矩阵 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission ...
- 2017ecjtu-summer training # 11 POJ 2492
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 38280 Accepted: 12452 D ...
- Genymotion的安装与使用(附百度云盘下载地址,全套都有,无需注册Genymotion即可使用)
http://blog.csdn.net/scythe666/article/details/70216144 附百度云盘下载地址 :http://pan.baidu.com/s/1jHPG7h8 1 ...
- Spark算子--cogroup
转载请标明出处http://www.cnblogs.com/haozhengfei/p/b612b1e6d9b951fad5574cd0ce573d7e.html cogroup--Transform ...