Here is Spring boot download file example using StreamingResponseBody. Using StreamingResponseBody download file using stream is possible. In this case server writing data to OutputStream at same time Browser read data. So   StreamingResponseBody writing and reading is possible to parallel. It will be very useful when large file download from the server.

Here is Spring boot download file example but this code will also work for spring MVC as well.

1. Using StreamingResponseBody

package com.javadeveloperzone.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by Its Spring boot download file using StreamingResponseBody it same like spring mvc download file  on 19-07-2017.
 */
@RestController  // Also possible to @Controller here
public class StreamingResponseBodyController {

    @RequestMapping(value = "downloadFile", method = RequestMethod.GET)
    public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"demo.pdf\"");
        InputStream inputStream = new FileInputStream(new File("C:\\demo-file.pdf"));
        return outputStream -> {
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                System.out.println("Writing some bytes..");
                outputStream.write(data, 0, nRead);
            }
        };

    }
}

Output:

Fire URL in browser it will download file. http://localhost:8080/downloadFile

Here is log which has been written in a console.

Writing some bytes..
Writing some bytes..
Writing some bytes..
Writing some bytes..
Writing some bytes..
Writing some bytes..
Writing some bytes..
Writing some bytes..
Spring boot download file example - output

Spring boot download file example – output

2. Download using InputStream to HttpServletResponse

Here we are writing file content to response (For Java 7 or older)

@RequestMapping(value = "downloadFile1", method = RequestMethod.GET)
   public void getSteamingFile1(HttpServletResponse response) throws IOException {
       response.setContentType("application/pdf");
       response.setHeader("Content-Disposition", "attachment; filename=\"demo.pdf\"");
       InputStream inputStream = new FileInputStream(new File("C:\\demo-file.pdf"));
           int nRead;
           while ((nRead = inputStream.read()) != -1) {
               response.getWriter().write(nRead);
           }
   }

3. Download using InputStreamResource

@RequestMapping(value = "downloadFile2", method = RequestMethod.GET)
  public InputStreamResource FileSystemResource (HttpServletResponse response) throws IOException {
      response.setContentType("application/pdf");
      response.setHeader("Content-Disposition", "attachment; filename=\"demo.pdf\"");
      InputStreamResource resource = new InputStreamResource(new FileInputStream("C:\\demo-file.pdf"));
      return resource;
  }

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-file-download-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring Boot Download File Example</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *