struts过滤器 中文乱码及上传乱码
package com.math.util.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;public class UpFilter implements Filter { private String charset; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub /**//*设置request的字符编码*/ req.setCharacterEncoding(charset); /**//*将request和response传回去,具体为什么请参考jsp基础*/ chain.doFilter(req, res); } public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub /**//*获取xml文件中初始化参数charset对应web.xml文件中的<param-name>charset</param-name>将值传给属性charset*/ charset = config.getInitParameter("charset"); }}在web.xml中设置<filter> <filter-name>upEncoding</filter-name> <filter-class>com.math.util.filter.UpFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>utf-8</param-value> </init-param></filter><filter-mapping> <filter-name>upEncoding</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
?