1 package com.it.demo6; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 import java.util.Map; 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletRequestWrapper; 14 15 /** 16 * 解决get和post请求 全部乱码 17 * 18 * @author 店小二的酒 19 * 20 */ 21 public class GenericEncodingFilter implements Filter { 22 23 @Override 24 public void destroy() { 25 } 26 27 @Override 28 public void doFilter(ServletRequest request, ServletResponse response, 29 FilterChain chain) throws IOException, ServletException { 30 // 转型为与协议相关对象 31 HttpServletRequest httpServletRequest = (HttpServletRequest) request; 32 // 对request包装增强 33 HttpServletRequest myrequest = new MyRequest(httpServletRequest); 34 chain.doFilter(myrequest, response); 35 } 36 37 @Override 38 public void init(FilterConfig filterConfig) throws ServletException { 39 } 40 41 } 42 43 // 自定义request对象 44 class MyRequest extends HttpServletRequestWrapper { 45 46 private HttpServletRequest request; 47 48 public MyRequest(HttpServletRequest request) { 49 super(request);// super必须写 50 this.request = request; 51 } 52 53 // 对需要增强方法 进行覆盖 54 @Override 55 public Map getParameterMap() { 56 // 先获得请求方式 57 String method = request.getMethod(); 58 if (method.equalsIgnoreCase("post")) { 59 // post请求 60 try { 61 // 处理post乱码 62 request.setCharacterEncoding("utf-8"); 63 return request.getParameterMap(); 64 } catch (UnsupportedEncodingException e) { 65 e.printStackTrace(); 66 } 67 } else if (method.equalsIgnoreCase("get")) { 68 // get请求 69 Map<String, String[]> parameterMap = request.getParameterMap(); 70 for (String parameterName : parameterMap.keySet()) { 71 String[] values = parameterMap.get(parameterName); 72 if (values != null) { 73 for (int i = 0; i < values.length; i++) { 74 try { 75 // 处理get乱码 76 if(values[i] != null){ 77 values[i] = new String(values[i].getBytes("ISO-8859-1"), "utf-8"); 78 } 79 } catch (UnsupportedEncodingException e) { 80 e.printStackTrace(); 81 } 82 } 83 } 84 } 85 return parameterMap; 86 } 87 88 return super.getParameterMap(); 89 } 90 91 @Override 92 public String getParameter(String name) { 93 String method = request.getMethod(); 94 if("get".equalsIgnoreCase(method)){ 95 String value = request.getParameter(name); 96 if (value != null) { 97 try { 98 value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); 99 } catch (UnsupportedEncodingException e) { 100 e.printStackTrace(); 101 } 102 } 103 return value; 104 }else if("post".equalsIgnoreCase(method)){ 105 try { 106 request.setCharacterEncoding("UTF-8"); 107 } catch (UnsupportedEncodingException e) { 108 e.printStackTrace(); 109 } 110 } 111 return super.getParameter(name); 112 } 113 114 @Override 115 public String[] getParameterValues(String name) { 116 Map<String, String[]> parameterMap = getParameterMap(); 117 String[] values = parameterMap.get(name); 118 return values; 119 } 120 121 }
稍微测试了下,没有错误!具体怎么样不知道!