1. 程式人生 > >http請求內容壓縮學習筆記

http請求內容壓縮學習筆記

http請求內容壓縮學習筆記

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //壓縮 public class Dmo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "bbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; System.out.println("壓縮前" + data.length()); // 以下代碼完成將String類型壓縮到byte[]中 ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(data.getBytes()); gout.flush(); gout.close(); // 取出壓縮後的數據 byte[] buf = bout.toByteArray(); System.out.println("壓縮後" + buf.length); // 將壓縮後的數據輸出到瀏覽器 response.setHeader("content-encoding", "gzip"); response.setHeader("content-length", buf.length + ""); // 服務端以字節方式輸出 response.getOutputStream().write(buf); } }

訪問:
技術分享圖片

技術分享圖片

http請求內容壓縮學習筆記