WEB开发网
开发学院软件开发Java 全站压缩 过滤器让访问速度提高减轻服务器负担 阅读

全站压缩 过滤器让访问速度提高减轻服务器负担

 2012-05-13 15:19:45 来源:WEB开发网   
核心提示:package cn.itcast.filter;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import jav
package cn.itcast.filter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

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;
import javax.servlet.http.HttpServletResponseWrapper;

public class GZIPFilter implements Filter {

	public void destroy() {
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//要想压缩页面 就是当用户访问页面时 在服务器把页面信息打给浏览器的时候进行压缩 所以这时候就是对response进行包装
		MyResponse myresponse = new MyResponse((HttpServletResponse) response);
		chain.doFilter(request, (ServletResponse) myresponse);
		//从包装的myresponse里获得写到容器流里的原始数据bs
		byte[] bs = myresponse.getOut();
		System.out.println("压缩前>>>"+bs.length);
		//用原始的response响应用户,此response需要转换为HttpServletResponse类型
		HttpServletResponse resp = (HttpServletResponse) response;
		//response解决中文乱码问题
		resp.setContentType("text/html;charset=utf-8");
		//开始压缩
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream zip = new GZIPOutputStream(out);
		zip.write(bs);
		zip.close();
		//获取压缩后的数据
		byte[] dest = out.toByteArray();
		System.out.println("压缩后>>>"+dest.length);
		//设置响应头
		resp.setDateHeader("content-length", dest.length);
		resp.setHeader("content-Encoding", "gzip");
		resp.getOutputStream().write(dest);
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

}
class MyResponse extends HttpServletResponseWrapper{
	private ByteArrayOutputStream out = new ByteArrayOutputStream();//定义一个容器
	public MyResponse(HttpServletResponse response) {
		super(response);
	}
	//重写父类的getwriter方法
	 PrintWriter writer;
	@Override
	public PrintWriter getWriter() throws IOException {
		//写到一个容器流里 	,
		writer = new PrintWriter(new OutputStreamWriter(out,"utf-8"), true);
		return writer;
	}
	//提供一个获得流里内容的方法    因为用字符流写到容器里的,有缓冲区,所以在取出流里的数据时判断字符流是否关闭
	public byte[] getOut(){
		if(writer!=null){
			writer.close();
		}
		return out.toByteArray();
	}	
}


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <filter>
  	<filter-name>GZIP</filter-name>
  	<filter-class>cn.itcast.filter.GZIPFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>GZIP</filter-name>
  	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Tags:全站 压缩 过滤器

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接