当前位置:网站首页>Period compression filter

Period compression filter

2022-07-06 19:49:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

1. Simple demonstration of compressed sampling points

public class ServletDemo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
		
		// Find a memory buffer byte stream 
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// Compress the data into the buffered byte stream 
		GZIPOutputStream gout = new GZIPOutputStream(baos);
		// Take out the data : Compressed 
		byte b[] = data.getBytes();// Raw bytes 
		System.out.println(" Original data size :"+b.length);
		gout.write(b);
		gout.close();// Ensure that all data enters the memory cache stream 
		
		// Take out the compressed data 
		b = baos.toByteArray();
		System.out.println(" Compressed data size :"+b.length);
		// Be sure to inform before output client Compression way 
		response.setHeader("Content-Encoding", "gzip");
		response.setContentLength(b.length);// inform client The size of the body 
		// use server Response object output 
		ServletOutputStream out = response.getOutputStream();
		out.write(b);
	}

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

		doGet(request, response);
	}

}

2. A simple demonstration example of the compression filter of the whole station

// Total station compression filter 
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);// release 
		// The compression code is written here 
		// Find a memory buffer byte stream 
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// Compress the data into the buffered byte stream 
		GZIPOutputStream gout = new GZIPOutputStream(baos);
		// Take out the data : Compressed 
		byte b[] = gresponse.getOldBytes();// Raw bytes 
		System.out.println(" Original data size :"+b.length);
		gout.write(b);
		gout.close();// Ensure that all data enters the memory cache stream 
		
		// Take out the compressed data 
		b = baos.toByteArray();
		System.out.println(" Compressed data size :"+b.length);
		// Be sure to inform before output client Compression way 
		response.setHeader("Content-Encoding", "gzip");
		response.setContentLength(b.length);// inform client The size of the body 
		// use server Response object output 
		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);
	}
	// Encapsulate the raw data into a buffer stream 
	@Override
	public ServletOutputStream getOutputStream() throws IOException {
		return new MyServletOutputStream(baos);
	}
	
	// Character stream : Encapsulate the raw data into a buffer stream 
	@Override
	public PrintWriter getWriter() throws IOException {
		pw = new PrintWriter(new OutputStreamWriter(baos, super.getCharacterEncoding()));// When characters flow into byte stream, the encoding will be lost 
		return pw;
	}
	// return baos Cache data in : original 
	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);
	}
}

Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117145.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061146429070.html