Question: Currently, I'm using this code in order to download a file:

```
ResponseEntity<Resource> response = this.restTemplate
    .getForEntity(
        uriToCall,
        Resource.class
    );

```

The problem is that code raises an `OutOfMemoryError` when I'm trying to download large files.

Is there any other way to get my large file avoiding this error?

**EDIT:**
Another question.

I was thinking about applying `FileSystemResource` instead of `Resource`:

```
ResponseEntity<FileSystemResource> resp = this.restTemplate
    .getForEntity(
        uriToCall,
        FileSystemResource.class
    );

```

Which is the difference between `FileSystemResource` and `Resource`? That class is going to accomplish my goal?

Answer: 
For large files a better way is to use streams, so the size would only be limited by available disk space.
Something like that should work:

```
this.restTemplate.execute(uriToCall, HttpMethod.GET, null, clientHttpResponse -> {
    File file= File.createTempFile("download", "tmp");
    StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(file));
});

```

See also this [example](https://www.baeldung.com/spring-resttemplate-download-large-file) for pause and resume.
See the following for answer to similar issue:

[How to forward large files with RestTemplate?](https://stackoverflow.com/questions/15781885/how-to-forward-large-files-with-resttemplate/32697516#32697516)

Create your RestTemplate as follows:

```
RestTemplate restTemplate = new RestTemplate();

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

```

This should resolve your out of memory issues.