当前位置:网站首页>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 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
// 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
边栏推荐
- Phoenix Architecture 3 - transaction processing
- 【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
- MySql必知必会学习
- redisson bug分析
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- 深度剖析原理,看完这一篇就够了
- 从sparse.csc.csr_matrix生成邻接矩阵
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
- VMware virtual machine cannot open the kernel device "\.\global\vmx86"
猜你喜欢

Example of applying fonts to flutter

Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发

Blue Bridge Cup microbial proliferation C language
腾讯T2大牛亲自讲解,跳槽薪资翻倍
![[玩转Linux] [Docker] MySQL安装和配置](/img/04/6253ef9fdf7d2242b42b4c7fb2c607.png)
[玩转Linux] [Docker] MySQL安装和配置

凤凰架构3——事务处理

LeetCode_双指针_中等_61. 旋转链表

Standardized QCI characteristics

Information System Project Manager - Chapter VIII project quality management
随机推荐
小微企业难做账?智能代账小工具快用起来
学习探索-函数防抖
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
企业精益管理体系介绍
How to access localhost:8000 by mobile phone
语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech
Hudi vs Delta vs Iceberg
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
Tensorflow2.0 self defined training method to solve function coefficients
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
JDBC details
MySQL information Schema Learning (i) - - General table
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
学习打卡web
力扣101题:对称二叉树
Interview assault 63: how to remove duplication in MySQL?
PowerPivot——DAX(初识)
Spark foundation -scala
Unbalance balance (dynamic programming, DP)
logstash高速入口