request 中文乱码解决方法

POST请求

request.setCharacterEncoding("UTF-8");

 

GET和POST请求

页面:UTF-8
Tomcat:iso8859-1

解决思路:
页面(utf-8编码) - Tomcat(iso8859-1解码)
先iso8859-1编码,再用utf-8解码
String str = request.getParameter("");
new String(str.getBytes("iso8859-1"),"UTF-8")

 

附录:

编码:
getBytes(String charsetName) 对字符串按照 charsetName 进行编码(unicode→charsetName),返回编码后的字节。 getBytes() 表示按照系统默认编码方式进行。

解码: String(
byte bytes[], Charset charset) 对字节按照 charset 进行解码(charset→unicode),返回解码后的字符串。 String(byte bytes[]) 表示按照系统默认编码方式进行

 

posted @ 2019-08-03 11:28  Archer_w  阅读(2264)  评论(0编辑  收藏  举报