Filter解決POST,GET提交中文亂碼問題
阿新 • • 發佈:2018-12-12
中文亂碼是一個非常煩人的問題,沒有學習Filter之前,每次都要進行轉碼
request.setCharacterEncoding("UTF-8");get還要進行轉碼.很是麻煩啊.後來學習到了Filter一下就可以只寫一次,再也不用寫了.媽媽再也不用擔心我的亂碼問題了.
另外說一下,tomcat8.0預設是UFT-8不用進行轉碼...
1.這個是用包裝類,也可以用動態代理
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /** * 解決get和post請求 全部亂碼 */ public class GenericEncodingFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 轉型為與協議相關物件 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 對request包裝增強 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } // 自定義request物件 class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; private boolean hasEncode; public MyRequest(HttpServletRequest request) { super(request);// super必須寫 this.request = request; } // 對需要增強方法 進行覆蓋 @Override public Map getParameterMap() { // 先獲得請求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { // post請求 try { // 處理post亂碼 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get請求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 確保get手動編碼邏輯只執行一次 for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values != null) { for (int i = 0; i < values.length; i++) { try { // 處理get亂碼 values[i] = new String(values[i] .getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } hasEncode = true; } return parameterMap; } return super.getParameterMap(); } @Override public String getParameter(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); if (values == null) { return null; } return values[0]; // 取回引數的第一個值 } @Override public String[] getParameterValues(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } }
2.動態代理的程式碼就少了一些
import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class EncodingFilter implements Filter { public EncodingFilter() { } public void destroy() { } public void init(FilterConfig fConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest req = (HttpServletRequest) request; HttpServletRequest myReq = (HttpServletRequest) Proxy.newProxyInstance( EncodingFilter.class.getClassLoader(),req.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; if(method.getName().equalsIgnoreCase("getParameter")) { String reqMethod = req.getMethod(); if("get".equalsIgnoreCase(reqMethod)) { String str = (String) method.invoke(req, args); System.out.println("GET "+str); String strUTF = new String(str.getBytes("ISO-8859-1"),"UTF-8"); System.out.println("GET "+strUTF); return strUTF; }else { req.setCharacterEncoding("UTF-8"); obj = method.invoke(obj, args); } }else { obj = method.invoke(req , args); } return obj; } }); chain.doFilter(myReq, response); } }