Home > Java > javaTutorial > How to Download a File from a Spring Boot REST Service: Why InputStreamReader Fails and How to Fix It with InputStreamResource or ByteArrayResource?

How to Download a File from a Spring Boot REST Service: Why InputStreamReader Fails and How to Fix It with InputStreamResource or ByteArrayResource?

Susan Sarandon
Release: 2024-11-02 07:37:02
Original
371 people have browsed it

How to Download a File from a Spring Boot REST Service: Why InputStreamReader Fails and How to Fix It with InputStreamResource or ByteArrayResource?

Downloading a File from Spring Boot REST Service

This article addresses the issue of downloading a file from a Spring Boot REST service effectively. The code provided below attempts to facilitate this functionality:

<code class="java">@RequestMapping(path="/downloadFile",method=RequestMethod.GET)
@Consumes(MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InputStreamReader> downloadDocument(
                String acquistionId,
                String fileType,
                Integer expressVfId) throws IOException {
        File file2Upload = new File("C:\Users\admin\Desktop\bkp\1.rtf");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        InputStreamReader i = new InputStreamReader(new FileInputStream(file2Upload));
        System.out.println("The length of the file is : "+file2Upload.length());

        return ResponseEntity.ok().headers(headers).contentLength(file2Upload.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(i);
        }</code>
Copy after login

However, upon attempting to download the file through a browser, the process initiates but ultimately fails. The following options can be explored to address this issue:

Option 1: Utilizing an InputStreamResource

An InputStreamResource can be employed to represent an InputStream. It should be considered when other specific Resource implementations are not applicable.

<code class="java">@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {

    // ...

    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(resource);
}</code>
Copy after login

Option 2: Using a ByteArrayResource

As recommended by the documentation for InputStreamResource, a ByteArrayResource can also be employed.

<code class="java">@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {

    // ...

    Path path = Paths.get(file.getAbsolutePath());
    ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

    return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(resource);
}</code>
Copy after login

The above is the detailed content of How to Download a File from a Spring Boot REST Service: Why InputStreamReader Fails and How to Fix It with InputStreamResource or ByteArrayResource?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template