简体   繁体   English

如何将大文件下载到字节数组中

[英]How to download large file into byte array

I am trying to download large file using spring Rest Template:我正在尝试使用 spring Rest模板下载大文件:

    private Result download(Long id) throws Exception {
    Result result = new Result();
    try {
        RestTemplate restTemplate = restTemplateFactory.getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.set(AUTHORIZATION, authorizationBL.getAccessToken().getAccessToken());
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(null, headers);
        Map<String, Long> valuesMap = new HashMap<String, Long>(1);
        valuesMap.put(ID, id);
        ResponseEntity<byte[]> response = restTemplate.exchange(downloadURL, HttpMethod.GET, request, byte[].class);
        byte[] stream = response.getBody();
        result.setData(stream);
        result.setStatus(Result.Status.SUCCESS);
    } catch (Exception e) {
        throw e;
    }
    return result;
}

when file size is large lets say above 100 MB then I am getting issue:当文件大小很大时,可以说超过 100 MB,那么我遇到了问题:

java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236) ~[na:1.8.0_60]
at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:191) ~[na:1.8.0_60]
at org.springframework.http.converter.ByteArrayHttpMessageConverter.readInternal(ByteArrayHttpMessageConverter.java:59) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.http.converter.ByteArrayHttpMessageConverter.readInternal(ByteArrayHttpMessageConverter.java:38) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:193) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:924) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:908) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:662) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:538) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]

I increased heap size upto 10g but it also didn't help.我将堆大小增加到 10g,但也无济于事。

Instead of downloading the file into a byte array, save it to disk.不是将文件下载到字节数组中,而是将其保存到磁盘。 Then you can read it from wherever else you need to in your program.然后,您可以从程序中需要的任何其他地方读取它。 For example, changing your method to例如,将您的方法更改为

/**
 * returns a File corresponding to the tmp file download for this resource
 */
private File download(Long id) throws IOException {
    File tmp = null;
    try {
        RestTemplate restTemplate = restTemplateFactory.getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.set(AUTHORIZATION, authorizationBL.getAccessToken().getAccessToken());
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(null, headers);
        ResponseEntity<byte[]> response = restTemplate.exchange(downloadURL, HttpMethod.GET, request, byte[].class);

        if (response.getStatusCode() == HttpStatus.OK) {
            tmp = Files.createTempFile("yourprefix", "yoursuffix");
            Files.write(tmp.toPath(), response.getBody());
        } else {
            // TODO handle this case
        }
    } catch (Exception e) {
        throw new IOException("An exception occurred while downloading", e);
    }
    return tmp;
}

A few observations:一些观察:

  1. The valuesMap wasn't used anywhere (also removed) valuesMap未在任何地方使用(也已删除)
  2. result also removed result也被删除

我能够使用流而不是将文件存储在 byte[] 中并使用IOUtils.copyLarge(inputStream, outputStream)来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM