在我使用get请求进行查询的时候遇到一个问题:

当我的请求参数中有中文时,出现乱码。

可是即使我设置了Spring的characterEncodingFilter,也还是出现乱码。

原因:tomcat默认使用ISO8859-1编码来解析get中的url参数,导致乱码。而characterEncodingFilter或者 request.setCharacterEncoding("UTF-8");都只针对post请求体有效。

下面对Http中get方法编码到tomcat的解码过程进行探究。

解决方法

  1. 更改tomcat中get方法默认ISO8859-1编码为utf-8编码。

    找到conf/server.xml,在 <Connector port="8082" protocol="HTTP/1.1" 中加入 URIEncoding="utf-8"
  2. 将参数以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方法的字符编码

  1. 如果在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>
  2. 如果没有进行上面的配置,那么从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。

参考文档

  1. 关于URL编码
  2. get请求中url传参中文乱码问题--集锦

get请求中文乱码及get,post编码探究的更多相关文章

  1. Spring-解决请求中文乱码问题

    解决spring请求中文乱码问题 1.web.xml添加编码拦截器 <filter> <filter-name>CharacterEncoding</filter-nam ...

  2. 使用httpclient post请求中文乱码解决办法

    使用httpclient post请求中文乱码解决办法   在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码 ...

  3. get请求与post请求中文乱码问题的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  4. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  5. 解决Post请求中文乱码问题

    解决Post请求中文乱码问题 req.setChracterEncoding()要在获取请求参数前调用才有效,不然还是乱码

  6. 5 Http请求中文乱码处理

    java 乱码分很多种,这里主要研究解决http请求中出现乱码的情况. http请求出现中文乱码的主要原因:发送方与接收方编码不一致,服务器默认支持的编码与web应用不一致,如:tomcat 是国外程 ...

  7. SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?

    在web.xml中 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-c ...

  8. Java中关于Servlet中请求中文乱码及文件下载

    1,Servlet请求响应中文乱码问题 package com.demo.servlet; import java.io.PrintWriter; import java.io.IOException ...

  9. java web中get请求中文乱码在filter中解决

    之前已经讲过get或者post方法的中文乱码问题,之前都是在每个方法中编写设置编码.如果程序变大,就会很繁琐,使用filter可以避免这种繁琐. 1)写一个encodingFilter进行编码设置 p ...

随机推荐

  1. dump_stack 分析使用

    dump_stack是用来回溯内核运行的信息的,打印内核信息堆栈段: dump_stack原型: void dump_stack(void); 1.使用这个功能时需要将内核配置勾选上: make me ...

  2. 基于springboot的freemarker创建指定格式的word文档

    在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...

  3. 论python3下“多态”与“继承”中坑

    1.背景: 近日切换到python3后,发现python3在多态处理上,有一些比较有意思的情况,特别记载,供大家参考... 以廖老师的python3教程中的animal 和dog的继承一节的代码做例子 ...

  4. JavaScript中数组map()方法

    JavaScript 数组map()方法创建一个新的数组使用调用此数组中的每个元素上所提供的函数的结果.语法 ? 1 array.map(callback[, thisObject]); 下面是参数的 ...

  5. [bzoj3955] [WF2013]Surely You Congest

    首先最短路长度不同的人肯定不会冲突. 对于最短路长度相同的人,跑个最大流就行了..当然只有一个人就不用跑了 看起来会T得很惨..但dinic在单位网络里是O(m*n^0.5)的... #include ...

  6. HDU_4883

    TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/O ...

  7. Elasticsearch介绍,一些概念的笔记

    Elasticsearch,分布式,高性能,高可用,可伸缩的搜索和分析系统 什么是搜索? 如果用数据库做搜索会怎么样? 什么是全文检索和Lucene? 什么是Elasticsearch? Elasti ...

  8. vi 方向键和Backspace键失效问题的解决方法

    安装的ubuntu默认的编辑器是vi,遇到了两个问题: ① insert模式下,按方向键将产生A.B.C.D等字符,解决方案: :set nocompatible ② insert模式下Backspa ...

  9. [国嵌攻略][051][NandFlash原理解析]

    扮演角色 相当于嵌入式设备的硬盘 NandFlash分类 1.SCL(single level cell):单层式存储 2.MLC(multi level cell):多层式存储 3.SCL在存储格上 ...

  10. Map,List,POJO深拷贝(序列化实现)方法与注意事项

    转载请注明出处,谢谢! 方法1: /*jdk >= 1.5*/ @SuppressWarnings("unchecked") public static <T> ...