当前位置:网站首页>句号压缩过滤器
句号压缩过滤器
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
边栏推荐
- 1805. 字符串中不同整数的数目
- Translation D28 (with AC code POJ 26:the nearest number)
- 广州首个数据安全峰会将在白云区开幕
- From spark csc. csr_ Matrix generate adjacency matrix
- Blue Bridge Cup microbial proliferation C language
- Application of clock wheel in RPC
- The slave i/o thread stops because master and slave have equal MySQL serv
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
- 1805. Number of different integers in the string
猜你喜欢
凤凰架构3——事务处理
Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
企业精益管理体系介绍
保证接口数据安全的10种方案
Application of clock wheel in RPC
Reflection and illegalaccessexception exception during application
激进技术派 vs 项目保守派的微服务架构之争
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Transformer model (pytorch code explanation)
It's enough to read this article to analyze the principle in depth
随机推荐
深度剖析原理,看完这一篇就够了
ZABBIX proxy server and ZABBIX SNMP monitoring
usb host 驱动 - UVC 掉包
Mysql Information Schema 學習(一)--通用錶
It's enough to read this article to analyze the principle in depth
Microservice architecture debate between radical technologists vs Project conservatives
信息系统项目管理师---第八章 项目质量管理
Spark foundation -scala
学习探索-无缝轮播图
short i =1; i=i+1与short i=1; i+=1的区别
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
About image reading and processing, etc
Problems encountered in using RT thread component fish
广州首个数据安全峰会将在白云区开幕
凤凰架构2——访问远程服务
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
121. 买卖股票的最佳时机
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
short i =1; I=i+1 and short i=1; Difference of i+=1
腾讯Android面试必问,10年Android开发经验