3

HTTP 応答が "Transfer-Encoding: Chunked" であるファイルをダウンロードする必要があります。これは、DataInputStream に新しいバイト バッファーを割り当てるために «getContentLength» を実行できないためです。正しく行う方法を教えてもらえますか?

コード例は非常に単純です。

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); }

4

1 に答える 1

1

がすべてのHttpURLConnectionデチャンクを処理します。ストリームの終わりまでバイトをコピーするだけです:

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

データを保存する場所outはどこですか。すべてがメモリに収まるわけではないため、これはお勧めできませんが、メモリ内で本当に必要な場合OutputStreamでも可能です。ByteArrayOutputStream

NBGETはすでにデフォルトのリクエスト方法です。設定する必要はありません。

于 2018-04-19T04:24:09.253 に答える