当前位置:网站首页>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
边栏推荐
- 【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
- Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
- 学习探索-函数防抖
- Introduction to enterprise lean management system
- MySQL information schema learning (I) -- general table
- Spark foundation -scala
- 【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
- Learning and Exploration - function anti shake
猜你喜欢

【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
腾讯T4架构师,android面试基础

MySQL information schema learning (II) -- InnoDB table

如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!

深入浅出,面试突击版

社招面试心得,2022最新Android高频精选面试题分享
算法面试经典100题,Android程序员最新职业规划

腾讯Android面试必问,10年Android开发经验
In depth analysis, Android interview real problem analysis is popular all over the network

企业精益管理体系介绍
随机推荐
RT-Thread 组件 FinSH 使用时遇到的问题
[pytorch] yolov5 train your own data set
MySQL information Schema Learning (i) - - General table
Application of clock wheel in RPC
350. Intersection of two arrays II
腾讯T3大牛手把手教你,大厂内部资料
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
Low CPU load and high loadavg processing method
Selenium advanced operations
从sparse.csc.csr_matrix生成邻接矩阵
350. 两个数组的交集 II
Example of applying fonts to flutter
After solving 2961 user feedback, I made such a change
利用 clip-path 绘制不规则的图形
AsyncHandler
About image reading and processing, etc
AddressSanitizer 技术初体验
凤凰架构3——事务处理
企业精益管理体系介绍
Tencent Android interview must ask, 10 years of Android development experience