对request进行增强,解决提交时出现的中文乱码问题。
public class CharacterEncodingFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("UTF-8"); //post
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//request.getParamter() requestProxy.getParameter()
chain.doFilter((ServletRequest) Proxy.newProxyInstance(CharacterEncodingFilter.class.getClassLoader(),request.getClass().getInterfaces(), new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(!method.getName().equals("getParameter")){
return method.invoke(request, args);
}
if(!request.getMethod().equalsIgnoreCase("get")){
return method.invoke(request, args);
}
//getParameter get
String value = (String) method.invoke(request, args);
if(value==null){
return null;
}
return new String(value.getBytes("iso8859-1"),"UTF-8");
}
}), response);
}
public void destroy() {}
public void init(FilterConfig filterConfig) throws ServletException {}
}
使用
动态代理,实现对response响应文本内容进行压缩
public class GzipFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
final HttpServletResponse response = (HttpServletResponse) resp;
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(bout,"UTF-8"));
//response.getWriter().write("aaa"); responseProxy
chain.doFilter(request, (ServletResponse) Proxy.newProxyInstance(GzipFilter.class.getClassLoader(), response.getClass().getInterfaces(), new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(method.getName().equals("getWriter")){
return pw;
}else if(method.getName().equals("getOutputStream")){ //servletOutputStream
return new MyServletOutputStream(bout);
}else{
return method.invoke(response, args);
}
}
}));
pw.close();
byte result[] = bout.toByteArray(); //拿到目标资源的输出
System.out.println("原始大小:" + result.length);
ByteArrayOutputStream bout2 = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout2);
gout.write(result);
gout.close();
byte gzip[] = bout2.toByteArray(); //拿到目标资源输出的压缩数据
System.out.println("压缩大小:" + gzip.length);
response.setHeader("content-encoding", "gzip");
response.setContentLength(gzip.length);
response.getOutputStream().write(gzip);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
class MyServletOutputStream extends ServletOutputStream{
private ByteArrayOutputStream bout = null;
public MyServletOutputStream(ByteArrayOutputStream bout){
this.bout = bout;
}
@Override
public void write(int b) throws IOException {
bout.write(b);
}
}