2018年1月25日 星期四

how to decode web content in gzip or deflate format using Java API?

有些網站回傳網頁會進行內容壓縮,壓縮方法常見有gzip或deflate,
可由回傳內容的ContentEncoding標頭決定如何處理回傳的壓縮內容。

以Java為例,解壓縮寫法如下:


      // 設定下載網址
      URL url = new URL("http://comment.bilibili.tv/29545595.xml");  
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
      //conn.setRequestProperty("Accept-Encoding", "identity");

      // 連線取得網頁輸入流
      conn.connect();  
      System.out.printf("con.getContentEncoding()=%s\n",conn.getContentEncoding());
      InputStream in = conn.getInputStream();

      if(conn.getContentEncoding().equals("gzip"))
          in = new GZIPInputStream(conn.getInputStream());  

      if(conn.getContentEncoding().equals("deflate"))
          in = new InflaterInputStream(conn.getInputStream(), new Inflater(true));

      // 從網頁輸入流列印內容到螢幕
      BufferedReader bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));  
      String s = null;  
      while((s=bin.readLine())!=null){  
         System.out.println(s);  
      }  
      bin.close();