Java: How to download chunked content correctly?

我只是一个虾纸丫 提交于 2019-12-23 17:33:35

问题


I have to download file which HTTP response is "Transfer-Encoding: Chunked", because of what I can't to «getContentLength» to allocate new bytes buffer for DataInputStream. Can you advice me how to do it correctly?

Code example is very simple:

try
{
       dCon = (HttpURLConnection) new URL(torrentFileDownloadLink.absUrl("href")).openConnection();
       dCon.setRequestProperty("Cookie", "session=" + cookies.get("session"));
       dCon.setInstanceFollowRedirects(false);
       dCon.setRequestMethod("GET");
       dCon.setConnectTimeout(120000);
       dCon.setReadTimeout(120000);

      // byte[] downloadedFile == ???

      DataInputStream br = new DataInputStream((dCon.getInputStream()));
      br.readFully(downloadedFile);
      System.out.println(downloadedFile);

} catch(IOException ex) { Logger.getLogger(WhatCDWork.class.getName()).log(Level.SEVERE, null, ex); }


回答1:


The HttpURLConnection will take care of all the de-chunking for you. Just copy the bytes until end of stream:

byte[] buffer = new  byte[8192];
int count;
while ((count = in.read( buffer)) > 0)
{
    out.write(buffer, 0, count);
}
out.close();
in.close();

where out is whatever OutputStream you want to save the data to. Could even be a ByteArrayOutputStream if you really need it in memory, although this isn't advisable as not everything fits into memory.

NB GET is already the default request method. You don't have to set it.



来源:https://stackoverflow.com/questions/5737945/java-how-to-download-chunked-content-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!