动态代理方式来解决全局乱码(get/post提交)

final HttpServletRequest req = (HttpServletRequest) request;
	// 使用动态代理完成全局编码
	HttpServletRequest enhanceRequest = (HttpServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(),
			req.getClass().getInterfaces(), new InvocationHandler() {

				public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
					// 对getParameter方法进行增强
					String name = method.getName();
					if ("getParameter".equals(name)) {
						String invoke = (String) method.invoke(req, args);// 乱码的字符串
						// 转码
						invoke = new String(invoke.getBytes("iso8859-1"), "utf-8");
						return invoke;
					}
					// 其他方法不管,原封不动的执行
					return method.invoke(req, args);
				}
			});
	chain.doFilter(enhanceRequest, response);

猜你喜欢

转载自blog.csdn.net/weixin_36964056/article/details/83691373