filter 过滤emoji
拦截器 public class EmojiFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter initialized"); this.filterConfig = filterConfig; } public void destroy() { System.out.println("Filter destroyed"); this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new MyRequestWrapper((HttpServletRequest) request), response); } }
web.xml配置 <!-- emoji过滤 --> <filter> <filter-name>EmojiFilter</filter-name> <filter-class>com.qtkj.base.EmojiFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EmojiFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
1 实际过滤方法 2 public class MyRequestWrapper extends HttpServletRequestWrapper { 3 public MyRequestWrapper(HttpServletRequest servletRequest) { 4 super(servletRequest); 5 } 6 7 public String[] getParameterValues(String parameter) { 8 String[] results = super.getParameterValues(parameter); 9 if (results == null) 10 return null; 11 12 int count = results.length; 13 String[] trimResults = new String[count]; 14 for (int i = 0; i < count; i++) { 15 trimResults[i] = com.qtkj.weixin.utils.EmojiFilter.filterEmoji(results[i].trim()); 16 trimResults[i]=trimResults[i].replace(" ", ""); 17 } 18 System.out.println("==========="+trimResults+"----------"); 19 return trimResults; 20 } 21 } 22 结合emoji处理方法中的方法