当前位置:网站首页>句号压缩过滤器
句号压缩过滤器
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
边栏推荐
- Blue Bridge Cup microbial proliferation C language
- Reflection and illegalaccessexception exception during application
- 【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
- IC设计流程中需要使用到的文件
- Transformer model (pytorch code explanation)
- Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
- 《数字经济全景白皮书》保险数字化篇 重磅发布
- Leetcode 30. 串联所有单词的子串
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- 黑馬--Redis篇
猜你喜欢
![[玩转Linux] [Docker] MySQL安装和配置](/img/04/6253ef9fdf7d2242b42b4c7fb2c607.png)
[玩转Linux] [Docker] MySQL安装和配置

Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing

10 schemes to ensure interface data security
![[play with Linux] [docker] MySQL installation and configuration](/img/04/6253ef9fdf7d2242b42b4c7fb2c607.png)
[play with Linux] [docker] MySQL installation and configuration
![[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.](/img/09/106adc222c06cbd2f4f66cf475cce2.jpg)
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.

腾讯T3大牛手把手教你,大厂内部资料

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting

Dark horse -- redis

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

Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
随机推荐
Application of clock wheel in RPC
Tencent Android interview must ask, 10 years of Android development experience
Hudi vs Delta vs Iceberg
MySQL information schema learning (I) -- general table
Black Horse - - Redis Chapter
[play with Linux] [docker] MySQL installation and configuration
Transformer model (pytorch code explanation)
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
凤凰架构2——访问远程服务
Cereals Mall - Distributed Advanced p129~p339 (end)
C # use Marshall to manually create unmanaged memory in the heap and use
广州首个数据安全峰会将在白云区开幕
深度剖析原理,看完这一篇就够了
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
ZABBIX proxy server and ZABBIX SNMP monitoring
Using clip path to draw irregular graphics
激进技术派 vs 项目保守派的微服务架构之争
Use of deg2rad and rad2deg functions in MATLAB
Test Li hi
The slave i/o thread stops because master and slave have equal MySQL serv