View Javadoc
1   package gboat2.base.plugin.servlet.filter;
2   
3   import static gboat2.base.bridge.GboatAppConstants.ENCODING_ISO88591;
4   import static gboat2.base.bridge.GboatAppConstants.ENCODING_UTF8;
5   
6   import java.io.IOException;
7   import java.net.URLDecoder;
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Iterator;
11  
12  import javax.servlet.FilterChain;
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServletRequest;
15  import javax.servlet.http.HttpServletResponse;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.springframework.web.filter.OncePerRequestFilter;
20  
21  /**
22   * 处理请求编码的过滤器,解决 get 方式提交乱码问题。<br>
23   * 在 web.xml 中添加如下配置启用该过滤器:
24   * <pre><code>
25   * &lt;!-- 编码过滤器 --&gt;
26   * &lt;filter&gt;
27   *     &lt;filter-name&gt;gboat2EncodingFilter&lt;/filter-name&gt;
28   *     &lt;filter-class&gt;gboat2.base.plugin.servlet.filter.GboatEncodingFilter&lt;/filter-class&gt;
29   * &lt;/filter&gt;
30   * &lt;filter-mapping&gt;
31   *     &lt;filter-name&gt;gboat2EncodingFilter&lt;/filter-name&gt;
32   *     &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
33   * &lt;/filter-mapping&gt;
34   * </code></pre>
35   * @date 2012-4-17
36   * @author zhangxj-a
37   * @since 1.0
38   */
39  public class GboatEncodingFilter extends OncePerRequestFilter {
40  
41      private Logger logger = LoggerFactory.getLogger(GboatEncodingFilter.class.getName());
42  
43      @Override
44      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
45              throws ServletException, IOException {
46          HttpServletRequest httpServletRequest = (HttpServletRequest) request;
47          request.setCharacterEncoding(ENCODING_UTF8);
48  
49          ArrayList<String> queryValues = new ArrayList<String>();
50          String query = httpServletRequest.getQueryString();
51          if (query != null) {
52              String[] expressions = query.split("&");
53              for (String expression : expressions) {
54                  String[] parts = expression.split("=");
55                  if (parts.length == 2) {
56                      String str = URLDecoder.decode(parts[1], ENCODING_UTF8);
57                      queryValues.add(new String(str.getBytes(ENCODING_UTF8), ENCODING_ISO88591));
58                      logger.debug(" request get parameter query key : {},value : {}", parts[0], str);
59                  }
60              }
61          }
62  
63          Collection<?> values = request.getParameterMap().values();
64          for (Iterator<?> iterator = values.iterator(); iterator.hasNext();) {
65              String[] strs = (String[]) iterator.next();
66              for (int i = 0; i < strs.length; i++) {
67                  for (String qStr : queryValues) {
68                      if (qStr.equals(strs[i])) {
69                          strs[i] = new String(qStr.getBytes(ENCODING_ISO88591), ENCODING_UTF8);
70                          break;
71                      }
72                  }
73              }
74          }
75  
76          filterChain.doFilter(request, response);
77      }
78  }