java防乱码_java过滤器防止乱码,

EncodingFilter

filter.EncodingFilter

charset

utf-8

EncodingFilter

/*

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;     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值