春RestTemplateでダウンロードを再開するには?

lucidMonkey:

私は(350メガバイト程度)ネクサスサーバーからファイルをダウンロードするRestTemplateを使用しています。で提供されているコードこの記事は、その目的に適しています:

RestTemplate restTemplate // = ...;

// Optional Accept header
RequestCallback requestCallback = request -> request.getHeaders()
        .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));

// Streams the response instead of loading it all in memory
ResponseExtractor<Void> responseExtractor = response -> {
    // Here I write the response to a file but do what you like
    Path path = Paths.get("some/path");
    Files.copy(response.getBody(), path);
    return null;
};
restTemplate.execute(URI.create("www.something.com"), HttpMethod.GET, requestCallback, responseExtractor);

私は、ファイルが存在する場合、レジュームダウンロードしようと、その後、チェックしたいと思います:

...
if(Files.exists(path)) {
    log.info("{} exists. Attempting to resume download", path);
    Files.write(path, StreamUtils.copyToByteArray(response.getBody()), StandardOpenOption.APPEND);
} else {
    Files.copy(response.getBody(), path);
}

しかし、これはただ単にOOMエラーになり:

java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source) ~[na:1.8.0_191]
    at java.io.ByteArrayOutputStream.grow(Unknown Source) ~[na:1.8.0_191]
    at java.io.ByteArrayOutputStream.ensureCapacity(Unknown Source) ~[na:1.8.0_191]
    at java.io.ByteArrayOutputStream.write(Unknown Source) ~[na:1.8.0_191]
    ...

私はカールとRangeヘッダを使用して通話をテストし、ネクサスはそれをサポートしている確信しています。私はそれをこのように設定しています:

long bytes = path.toFile().length();
...
request.getHeaders().setRange(Arrays.asList(HttpRange.createByteRange(bytes)));

私は、メモリエラーが上記のInputStreamブロックするために発生推測しています。代わりに、チャンネル/バッファを使用しようと、私はそう:

...
try {
    if(Files.exists(path)) {
        log.info("{} exists. Attempting to resume download", path);
        ReadableByteChannel channel = Channels.newChannel(response.getBody());

        FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.APPEND);
        fileChannel.tryLock();
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        int bytesRead = 0;
        while((bytesRead = channel.read(buffer)) != -1) {
            fileChannel.write(buffer);
            buffer.clear();
        }

        fileChannel.close();    
    } else {
        Files.copy(response.getBody(), path);
    }
...

これは、少なくとも書き込みをいくつかのファイルにデータをまだ失敗します。任意のヘルプは高く評価されるように、私はjava.nioの設備を備えた多くの経験を持っていません。

*編集:すべての答えが理解されているが、私はこのプロジェクトのためにJDK 8を使用するように強制しています。

Alexandarペトロフ:

あなたがメモリに読み出されない流れを確保するための大きなファイルで作業している場合は、次のコード行が必要になります。

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);     
restTemplate.setRequestFactory(requestFactory);     

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=219749&siteId=1