GZIP:response压缩
来源:互联网 发布:鲸蜡醇乙基己酸酯 知乎 编辑:程序博客网 时间:2024/06/10 04:08
设置编码问题
response压缩
public class GzipFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request; HttpServletResponse response; try{ request = (HttpServletRequest)req; response = (HttpServletResponse)resp; }catch(Exception e){ throw new RuntimeException("non-http request"); } //对 response进行包装,为了能获取向浏览器写的数据 MyHttpServletResponse myresponse = new MyHttpServletResponse(response); chain.doFilter(request, myresponse); //压缩代码应放到放行后 byte b[] = myresponse.getBytes();//如何拿到原始的数据字节序列 System.out.println("压缩前大小:"+b.length); ByteArrayOutputStream out = new ByteArrayOutputStream();//内存数组流:缓存输出流 GZIPOutputStream gout = new GZIPOutputStream(out); gout.write(b); gout.close();// 压缩后的数据到ByteArrayOutputStream中了 b = out.toByteArray();//取出压缩后的数据 System.out.println("压缩后大小:"+b.length); //通知浏览器正文的压缩格式 response.setHeader("Content-Encoding", "gzip"); response.getOutputStream().write(b); } public void destroy() { }}class MyHttpServletResponse extends HttpServletResponseWrapper{ private HttpServletResponse response; private ByteArrayOutputStream baos = new ByteArrayOutputStream();//输出的数据 private PrintWriter pw; public MyHttpServletResponse(HttpServletResponse response){ super(response); this.response = response; } //字节流.覆盖该方法的目的,截获原始的数据:当外部调用getOutputStream()时,能获取输出流的内容数据 @Override public ServletOutputStream getOutputStream() throws IOException { return new MyServletOutputStream(baos); } //字符流输出 当外部调用getWriter()时,能获取输出流的内容数据 @Override public PrintWriter getWriter() throws IOException { pw = new PrintWriter(new OutputStreamWriter(baos, response.getCharacterEncoding())); return pw; } /** * 刷新存放缓存的字节流输出的数据,得到写入的数据 * @return */ public byte[] getBytes(){ try { if(pw!=null){ pw.close(); } baos.flush(); } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); }}/** * 包装字节流 * @author zhangjianbin * */class MyServletOutputStream extends ServletOutputStream{ private ByteArrayOutputStream baos; public MyServletOutputStream(ByteArrayOutputStream baos){ this.baos = baos; } //将外部的数据写入 ByteArrayOutputStream 存放缓存的字节流输出的数据 @Override public void write(int b) throws IOException { baos.write(b); }}
public class ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "<h1>中中</h1>"; //response 是包装后的 ServletOutputStream sos = response.getOutputStream(); //调用了包装后的对象,这里是写入了自定义的内存缓存字节数组中去了 sos.write(data.getBytes("UTF-8"));// byte b[] = data.getBytes();// System.out.println("压缩前大小:"+b.length);// ByteArrayOutputStream out = new ByteArrayOutputStream();//内存数组流:缓存输出流// GZIPOutputStream gout = new GZIPOutputStream(out);// gout.write(b);// gout.close();// 压缩后的数据到ByteArrayOutputStream中了// // // b = out.toByteArray();//取出压缩后的数据// System.out.println("压缩后大小:"+b.length);// // //通知浏览器正文的压缩格式// response.setHeader("Content-Encoding", "gzip");// response.getOutputStream().write(b); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
0 0
- GZIP:response压缩
- Rails 支持 Response 数据 GZip 压缩 json
- 乱码被GZip压缩过的Response进行解压
- gzip压缩
- GZip压缩
- GZIP压缩
- Gzip压缩
- GZIP压缩
- Gzip压缩
- GZIP压缩
- GZIP --压缩
- Gzip压缩
- GZIP压缩
- Gzip压缩
- gzip压缩
- gzip压缩
- GZIP Http Servlet Response
- gzip压缩和解压缩
- python的egg制作
- 对ICMP错误消息的处理
- spark源码学习(一)---sparkContext(1)
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- 个人笔记 js 15 js字符串的转换
- GZIP:response压缩
- 十指相扣:陪binderIPC度过的漫长岁月(1)
- 生产者与消费者
- 32位float在内存中的存储主要分成三部分:1bit符号位,8bit指数位(127+指数),23bit尾数位..0.0f存0。。1.0f存0x3f800000.
- 【推荐】公共领域高质量公开数据集列表
- 我在小公司前端要做什么?
- JDBC之简单事务处理
- 这是一篇记事
- 2015年终总结