Filter实现压缩数据
package
cn.icoke.filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.zip.GZIPOutputStream;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
/*
*author xiaozhe
*
*
*/
public class GzipFilter implements Filter {
/**
* Constructor of the object.
*/
public GzipFilter() {
super ();
}
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this .filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain) {
try {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
ProxyResponse rpoxy = new ProxyResponse(response);
filterChain.doFilter(request, rpoxy.getResponse());
byte [] result = rpoxy.toByteArray();
System.out.println( " 压缩前的数据大小: " + result.length);
// 实现压缩数据
byte [] gzipResult = gzip(result);
System.err.println( " 压缩后的数据大小: " + gzipResult.length);
response.setHeader( " Content-Encoding " , " gzip " );
response.setHeader( " Content-Length " , gzipResult.length + "" );
response.getOutputStream().write(gzipResult);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
} // end method doFilter()
private byte [] gzip( byte [] result) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(result);
gout.close();
return out.toByteArray();
}
public void destroy() {
}
} // end class
/*
* response 装饰设计模式实现
*/
/*
class RaiseResponse extends HttpServletResponseWrapper{
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private PrintWriter pw = null;
private HttpServletResponse response =
(HttpServletResponse) super.getResponse();
public RaiseResponse(HttpServletResponse response) {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new RaiseOutputStream(out);
}
@Override
public PrintWriter getWriter() throws IOException {
//字符流在再转换成字节流是 注意要用转换流OutputStreamWriter去转换并设置编码集 不然会出现乱码现象
pw = new PrintWriter(new OutputStreamWriter(out,response.getCharacterEncoding()));
return pw;
}
public byte[] toByteArray(){
if(pw != null){
pw.close();
}
return out.toByteArray();
}
}//end class
class RaiseOutputStream extends ServletOutputStream{
ByteArrayOutputStream out = null;
public RaiseOutputStream(ByteArrayOutputStream out){
this.out = out;
}
@Override
public void write(int b) throws IOException {
out.write(b);
}
}//end class
*/
/*
*代理模式 实现数据压缩
*/
class ProxyResponse {
private HttpServletResponse response;
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private PrintWriter pw = null ;
public ProxyResponse(HttpServletResponse response){
this .response = response;
}
public HttpServletResponse getResponse(){
return (HttpServletResponse) Proxy.newProxyInstance(ProxyResponse. class .getClassLoader(),
response.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if (method.getName().equals( " getOutputStream " )){
return new RaiseServletOutputStream(out);
}
if (method.getName().equals( " getWriter " )){
pw = new PrintWriter(
new OutputStreamWriter(out,response.getCharacterEncoding()));
return pw;
}
return method.invoke(response, args);
}
});
} // end method
public byte [] toByteArray(){
if (pw != null ){
pw.close();
}
return out.toByteArray();
} // end method
} // end class
class RaiseServletOutputStream extends ServletOutputStream{
ByteArrayOutputStream out = null ;
public RaiseServletOutputStream(ByteArrayOutputStream out ){
this . out = out;
}
@Override
public void write( int b) throws IOException {
out.write(b);
}
}
/*
* 在配置一个 压缩功能的filter时要注意 <dispatcher>FORWARD</dispatcher>
* 拦截方式要大写 可以同时配置多个拦截方式
*
* <filter>
<filter-name>GzipFilter</filter-name>
<filter-class>cn.icoke.filter.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
*
*
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.zip.GZIPOutputStream;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
/*
*author xiaozhe
*
*
*/
public class GzipFilter implements Filter {
/**
* Constructor of the object.
*/
public GzipFilter() {
super ();
}
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this .filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain) {
try {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
ProxyResponse rpoxy = new ProxyResponse(response);
filterChain.doFilter(request, rpoxy.getResponse());
byte [] result = rpoxy.toByteArray();
System.out.println( " 压缩前的数据大小: " + result.length);
// 实现压缩数据
byte [] gzipResult = gzip(result);
System.err.println( " 压缩后的数据大小: " + gzipResult.length);
response.setHeader( " Content-Encoding " , " gzip " );
response.setHeader( " Content-Length " , gzipResult.length + "" );
response.getOutputStream().write(gzipResult);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
} // end method doFilter()
private byte [] gzip( byte [] result) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(result);
gout.close();
return out.toByteArray();
}
public void destroy() {
}
} // end class
/*
* response 装饰设计模式实现
*/
/*
class RaiseResponse extends HttpServletResponseWrapper{
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private PrintWriter pw = null;
private HttpServletResponse response =
(HttpServletResponse) super.getResponse();
public RaiseResponse(HttpServletResponse response) {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new RaiseOutputStream(out);
}
@Override
public PrintWriter getWriter() throws IOException {
//字符流在再转换成字节流是 注意要用转换流OutputStreamWriter去转换并设置编码集 不然会出现乱码现象
pw = new PrintWriter(new OutputStreamWriter(out,response.getCharacterEncoding()));
return pw;
}
public byte[] toByteArray(){
if(pw != null){
pw.close();
}
return out.toByteArray();
}
}//end class
class RaiseOutputStream extends ServletOutputStream{
ByteArrayOutputStream out = null;
public RaiseOutputStream(ByteArrayOutputStream out){
this.out = out;
}
@Override
public void write(int b) throws IOException {
out.write(b);
}
}//end class
*/
/*
*代理模式 实现数据压缩
*/
class ProxyResponse {
private HttpServletResponse response;
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private PrintWriter pw = null ;
public ProxyResponse(HttpServletResponse response){
this .response = response;
}
public HttpServletResponse getResponse(){
return (HttpServletResponse) Proxy.newProxyInstance(ProxyResponse. class .getClassLoader(),
response.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if (method.getName().equals( " getOutputStream " )){
return new RaiseServletOutputStream(out);
}
if (method.getName().equals( " getWriter " )){
pw = new PrintWriter(
new OutputStreamWriter(out,response.getCharacterEncoding()));
return pw;
}
return method.invoke(response, args);
}
});
} // end method
public byte [] toByteArray(){
if (pw != null ){
pw.close();
}
return out.toByteArray();
} // end method
} // end class
class RaiseServletOutputStream extends ServletOutputStream{
ByteArrayOutputStream out = null ;
public RaiseServletOutputStream(ByteArrayOutputStream out ){
this . out = out;
}
@Override
public void write( int b) throws IOException {
out.write(b);
}
}
/*
* 在配置一个 压缩功能的filter时要注意 <dispatcher>FORWARD</dispatcher>
* 拦截方式要大写 可以同时配置多个拦截方式
*
* <filter>
<filter-name>GzipFilter</filter-name>
<filter-class>cn.icoke.filter.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
*
*
*/