HttpServletRequest的.getParameter方法不支持解析中文,导致解析后乱码

295 阅读1分钟

问题提出

后端需要接收前端发送的中文字符串,HttpServletRequest的.getParameter方法不支持解析中文,导致解析后乱码。

转码前

问题解决

使用String类的构造方法,进行转码,例如:

String productName = request.getParameter("productName");
productName = new String(productName.getBytes("8859_1"), "utf8");   // 接收中文,转码

使用String类的构造方法进行转码

转码后