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:

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





Popular posts from this blog

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete