Spring Webflux File Download Example
Hello everyone, Here we will show you an example of how to download files in a Spring WebFlux application.
More related topics:
- Spring Webflux File Upload (Single/Multiple)- REST API Example
- Kotlin + Spring Webflux File Upload (Single/Multiple)- REST API Example
- Kotlin + Spring Webflux File Download - REST API Example
- Reactive Rest CRUD APIs using Spring Boot, WebFlux, and Reactive Mongo
Technologies Used:
- Spring Boot 2.5.4
- Spring Webflux 2.5.4
- Java 11
- Maven 3
Project Structure:
Project Dependency Management
Spring boot dependencies, no need for the extra library for file upload.
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.deno</groupId>
<artifactId>springwebfluxfiledownload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springwebfluxfiledownload</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Rest Controller
package com.knf.dev.deno.springwebfluxfiledownload.controller;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import java.io.File;
import java.io.IOException;
@RestController
public class DownloadFile {
@GetMapping("/download/{fileName}")
public Mono<Void> downloadFile(@PathVariable String fileName,
ServerHttpResponse response) throws IOException {
ZeroCopyHttpOutputMessage zeroCopyResponse =
(ZeroCopyHttpOutputMessage) response;
response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename="+fileName+"");
response.getHeaders().setContentType(MediaType.
APPLICATION_OCTET_STREAM);
ClassPathResource resource = new ClassPathResource(fileName);
File file = resource.getFile();
return zeroCopyResponse.writeWith(file, 0,
file.length());
}
}
Spring Main Driver
package com.knf.dev.deno.springwebfluxfiledownload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringwebfluxfiledownloadApplication {
public static void main(String[] args) {
SpringApplication.
run(SpringwebfluxfiledownloadApplication.class, args);
}
}
Local Environment Setup
Step1: Download or clone the source code - click here
Step2: mvn spring-boot:run
Step3: mvn spring-boot:run
Download the file