句号压缩过滤器

句号压缩过滤器

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

1.压缩采样点的简单演示

public class ServletDemo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
		
		//找一个内存缓冲字节流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//把数据压缩到缓冲字节流流中
		GZIPOutputStream gout = new GZIPOutputStream(baos);
		//取出数据:压缩后的
		byte b[] = data.getBytes();//原始字节
		System.out.println("原有数据大小:"+b.length);
		gout.write(b);
		gout.close();//保证全部的数据都进入内存缓存流
		
		//取出压缩后的数据
		b = baos.toByteArray();
		System.out.println("压缩后的数据大小:"+b.length);
		//输出前一定要告知client压缩方式
		response.setHeader("Content-Encoding", "gzip");
		response.setContentLength(b.length);//告知client正文的大小
		//用server的响应对象输出
		ServletOutputStream out = response.getOutputStream();
		out.write(b);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}

2.全站压缩过滤器的一个简单演示样例

//全站压缩过滤器
public class GzipFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		GzipHttpServletResponse gresponse = new GzipHttpServletResponse(response);
		chain.doFilter(request, gresponse);//放行
		//压缩代码写在此处
		//找一个内存缓冲字节流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//把数据压缩到缓冲字节流流中
		GZIPOutputStream gout = new GZIPOutputStream(baos);
		//取出数据:压缩后的
		byte b[] = gresponse.getOldBytes();//原始字节
		System.out.println("原有数据大小:"+b.length);
		gout.write(b);
		gout.close();//保证全部的数据都进入内存缓存流
		
		//取出压缩后的数据
		b = baos.toByteArray();
		System.out.println("压缩后的数据大小:"+b.length);
		//输出前一定要告知client压缩方式
		response.setHeader("Content-Encoding", "gzip");
		response.setContentLength(b.length);//告知client正文的大小
		//用server的响应对象输出
		ServletOutputStream out = response.getOutputStream();
		out.write(b);
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
class GzipHttpServletResponse extends HttpServletResponseWrapper{
	private ByteArrayOutputStream baos = new ByteArrayOutputStream();
	private PrintWriter pw;
	public GzipHttpServletResponse(HttpServletResponse response){
		super(response);
	}
	//把原始数据封装到一个缓冲流中
	@Override
	public ServletOutputStream getOutputStream() throws IOException {
		return new MyServletOutputStream(baos);
	}
	
	//字符流:把原始数据封装到一个缓冲流中
	@Override
	public PrintWriter getWriter() throws IOException {
		pw = new PrintWriter(new OutputStreamWriter(baos, super.getCharacterEncoding()));//字符流转成字节流编码会丢失
		return pw;
	}
	//返回baos中的缓存数据:原始
	public byte[] getOldBytes(){
		try {
			if(pw!=null){
				pw.close();
			}
			baos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return baos.toByteArray();
	}
	
}
class MyServletOutputStream extends ServletOutputStream{
	private ByteArrayOutputStream baos;
	public MyServletOutputStream(ByteArrayOutputStream baos){
		this.baos = baos;
	}
	@Override
	public void write(int b) throws IOException {
		baos.write(b);
	}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117145.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号