1. Post中文请求乱码只需要设置请求体的字符集为 UTF-8,就可以解决 post 请求的中文乱码问题
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {// 设置请求体的字符集为 UTF-8,从而解决 post 请求的中文乱码问题//这个API只有在获取请求参数之前调用才有效req.setCharacterEncoding("UTF-8");}
2.Get中文请求乱码只需要设置请求体的字符集为 UTF-8,就可以解决 post 请求的中文乱码问题
// 获取请求参数String username = req.getParameter("username");//1 先以 iso8859-1 进行编码//2 再以 utf-8 进行解码username = new String(username.getBytes("iso-8859-1"), "UTF-8");