当前位置:网站首页>句号压缩过滤器
句号压缩过滤器
2022-07-06 11:48:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
1.压缩采样点的简单演示
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//找一个内存缓冲字节流
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);
}
}版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117145.html原文链接:https://javaforall.cn
边栏推荐
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
- Mysql Information Schema 学习(一)--通用表
- [translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
- 【pytorch】yolov5 训练自己的数据集
- C # use Marshall to manually create unmanaged memory in the heap and use
- Mysql Information Schema 学习(二)--Innodb表
- In simple terms, interview surprise Edition
- Tensorflow2.0 自定义训练的方式求解函数系数
- 通俗的讲解,带你入门协程
- Alibaba数据源Druid可视化监控配置
猜你喜欢

After solving 2961 user feedback, I made such a change

MySQL information Schema Learning (i) - - General table

腾讯T3手把手教你,真的太香了

Reflection and illegalaccessexception exception during application

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

通俗的讲解,带你入门协程

【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。

《数字经济全景白皮书》保险数字化篇 重磅发布

Analysis of rainwater connection

Phoenix Architecture 3 - transaction processing
随机推荐
DaGAN论文解读
Use of deg2rad and rad2deg functions in MATLAB
Spark foundation -scala
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
1805. Number of different integers in the string
LeetCode_双指针_中等_61. 旋转链表
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
凤凰架构3——事务处理
信息系统项目管理师---第八章 项目质量管理
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
Mysql Information Schema 學習(一)--通用錶
Hudi vs Delta vs Iceberg
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
从sparse.csc.csr_matrix生成邻接矩阵
Simple application of VBA script in Excel
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
zabbix 代理服务器 与 zabbix-snmp 监控
Analysis of rainwater connection
short i =1; I=i+1 and short i=1; Difference of i+=1