Spring Webflux File Download - REST API Example

In this article, We will learn how to download files in a RESTful Spring WebFlux application.

More Spring WebFlux practice:

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());
}
}


Main Class
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
Step2: mvn spring-boot:run
Step3: mvn spring-boot:run


Download the file





Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example