中文字符转码

中文字符转码

1.    String s = “中文”;
S =  new String(s.getBytes(“ISO8859-1”),”utf-9”);
2.    使用过滤器:
public  class CharsetFilter  implements Filter{

     private String encoding = "UTF-8";
    
     public  void destroy() {
        
    }

     public  void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)  throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }

     public  void init(FilterConfig filterConfig)  throws ServletException {
        String encoding = filterConfig.getInitParameter("encoding");
         if(encoding !=  null) {
             this.encoding = encoding;
        }
    }

}
3.request.setCharacterEncoding(“utf-8”);

你可能感兴趣的:(中文字符转码)