Post请求中文乱码

对于单次请求添加request.setCharacterEncoding("utf-8");

使用spring提供的编码过滤器

POST/GET请求中文乱码问题

get请求中文乱码

tomcat配置文件添加编码与工程编码一致

<ConnectorURIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

对参数转码为utf-8

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

URLEncoder-URLDecoder加解密处理

在传入参数时对参数进行两次编码,服务端获取参数时进行一次解码(常用)

传入时:URLEncoder.encode(URLEncoder.encode(param,"UTF-8"),"UTF-8"))

服务器获取时:String param= URLDecoder.decode(param, "UTF-8");

相关文章: