java过滤器防止乱码,

来源:互联网 发布:分布式数据库关键技术 编辑:程序博客网 时间:2024/06/05 04:18

<filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>filter.EncodingFilter</filter-class>
 
  <init-param>
  <param-name>charset</param-name>
  <param-value>utf-8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>



package filter;


import java.io.IOException;


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.HttpServletResponse;


/**
 * 自定义字符集过滤器,解决用GET方式提交表单字符集出现的问题
 *
 * @author bhz
 * @version 1.0.1 
 */
public class EncodingFilter implements Filter {


    private FilterConfig filterConfig;


    private String chartSet = "UTF-8";


    private static final String FORM_METHOD_POST = "POST";


    private static final String FORM_METHOD_GET = "GET";


    public FilterConfig getFilterConfig() {
        return filterConfig;
    }


    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;


    }


    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        String filterCharSet = filterConfig.getInitParameter("charset");
        if (filterCharSet != null && filterCharSet.trim().length() != 0) {
            this.chartSet = filterCharSet;
        }
    }


    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String formMethod = request.getMethod();
        if (FORM_METHOD_POST.equalsIgnoreCase(formMethod)) {
            request.setCharacterEncoding(this.chartSet);
        } else if (FORM_METHOD_GET.equalsIgnoreCase(formMethod)) {
            request = new RequestUrlWrapper(request, chartSet);
        }
        response.setCharacterEncoding(this.chartSet);
        filterChain.doFilter(request, response);
    }


public void destroy() {
this.filterConfig = null;

}




}




package filter;


import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
/**
 * RequestUrlWrapper.java
 * 功能:包装请求url
 * 类名: RequestUrlWrapper
 * ---------------------------------------------------
 * V1.0.1    bhz    初始版本
 */
public class RequestUrlWrapper extends HttpServletRequestWrapper {


    private String charSet = "UTF-8";


    public static final String REQUEST_URL_ATTRIBUTE_NAME = "javax.servlet.forward.request_url.framework";


    public RequestUrlWrapper(HttpServletRequest httpServletRequest) {
        super(httpServletRequest);
    }




    public RequestUrlWrapper(HttpServletRequest httpServletRequest, String charSet) {
        super(httpServletRequest);
        this.charSet = charSet;
    }




    public String getParameter(String name) {
        try {
            super.setCharacterEncoding(this.charSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        String value = super.getParameter(name);
        value = value == null ? null : convertCharSet(value);
        return value;
    }




    public String convertCharSet(String target) {
        try {
            if ((!isGBK(target)) || " ".equals(target)) {
                return new String(target.trim().getBytes("ISO-8859-1"), this.charSet);
            } else {
                return target;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return target;
        }
    }




    public static String returnURL(HttpServletRequest request) throws Exception {
        String url = request.getAttribute(REQUEST_URL_ATTRIBUTE_NAME).toString();
        if (url == null) {
            url = request.getRequestURL().toString();
        }
        return url == null ? "" : url;
    }


    public static boolean isGBK(String string)
            throws java.io.UnsupportedEncodingException {
        byte[] bytes = string.replace('?', 'a').getBytes("ISO-8859-1");
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == 63) {
                return true;
            }
        }
        return false;
    }


    /**
     * 对于gb2312来讲,首字节码位从0×81至0×FE,尾字节码位分别是0×40至0×FE
     *
     * @param str
     * @return
     */
    public static boolean isGB2312(String str) {
        char[] chars = str.toCharArray();
        boolean isGB2312 = false;
        for (int i = 0; i < chars.length; i++) {
            byte[] bytes = ("" + chars[i]).getBytes();
            if (bytes.length == 2) {
                int[] ints = new int[2];
                ints[0] = bytes[0] & 0xff;
                ints[1] = bytes[1] & 0xff;
                if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40
                        && ints[1] <= 0xFE) {
                    isGB2312 = true;
                    break;
                }
            }
        }
        return isGB2312;
    }
}