Spring WebFlux File Upload

Sample Application:

  • single file
  • multiple files

Project Setup:

Spring WebFlux File Upload — Single File:

  • I first create a simple HTML file under resources/static directory which looks like this.
  • As part of submission, we send a POST request to upload/file/single endpoint.
  • Along with file, we can also send other information like user name.
  • HTML source is as shown here.
<div class="container mt-5">
<h1>Single File Upload Demo!</h1>
<form class="mt-3" action="upload/file/single" method="post" enctype="multipart/form-data">
<div class="form-group">
<label >User</label>
<input type="text" name="user-name"> <br/><br/>
<label >Single</label>
<input type="file" name="fileToUpload" id="fileToUpload1">
</div>
<div class="form-group mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
  • The back end controller is very simple. We look for the specific name field (in this case fileUpload & name field). We store the content on the server in a specific location.
  • When I run the application and upload the file, the file gets uploaded successfully on the server.

Spring WebFlux File Upload — Multiple Files:

  • If we expect the users of the application to upload multiple files, then the process is still same!! Instead of Mono, we need to use Flux.
<div class="container mt-5">
<h1>Multiple File Upload Demo!</h1>
<form class="mt-3" action="upload/file/multi" method="post" enctype="multipart/form-data">
<div class="form-group">
<label >Multi</label>
<input type="file" name="files" id="files" multiple>
</div>
<div class="form-group mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
  • Controller
  • When I tried to upload multiple files, I am able to receive them on the server side.

Summary:

--

--

--

Principal Software Engineer — passionate about software architectural design, microservices.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java Exception Handling

C and Functional Safety in the automotive industry, part 2: The usual suspects

CertiK Audits Deerfi LendingProtocol

Day #26 of Python

Pull container images in Azure Container Registry (ACR) from Azure VM with managed service identity…

Introduction to Linux OS

Mendix Mindfulness: The unused Microflow Parameter

How to open plot tools in MatLab?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinoth Selvaraj

Vinoth Selvaraj

Principal Software Engineer — passionate about software architectural design, microservices.

More from Medium

Spring WebFlux — Error Handling

Spring Batch Admin in Spring Boot 2

Start with Spring Boot

Getting started with Spring for GraphQL in Spring Boot