Spring WebFlux File Upload

Spring WebFlux is a non-blocking web stack to handle multiple concurrent requests with minimal number of threads and scale with fewer hardware resources. It performs much better compared Spring MVC when the application has to handle a lot of I/O requests.

In this particular tutorial, We are going to take a look at multipart File Upload example with WebFlux.

Sample Application:

  • single file
  • multiple files

Project Setup:

Spring WebFlux File Upload — Single File:

  • 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:

<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:

You can learn more about Spring WebFlux.

The complete source code is here.

Happy coding :)

--

--

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

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

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