In this Spring Boot file upload example you’ll learn how easy it is to move a file on the client’s machine to a folder on the server — all asynchronously — and with a minimal amount of code.

How to upload a file with Spring

Follow these five steps to use Spring to upload files from the client to the server:

  1. Create a Spring Boot application and include the Spring Web facilities;
  2. Create a Spring @Controller class;
  3. Add a method to the controller class which takes Spring’s MultipartFile as an argument;
  4. Save the uploaded file to a directory on the server; and
  5. Send a response code to the client indicating the Spring file upload was successful.

Spring MVC file upload controller

Developers should use the following code for the controller class, which handles the Spring file upload:

<span style="color: #999999">package com.example.demo;</span>





<span style="color: #999999">import java.io.File;</span>


<span style="color: #999999">import org.springframework.http.*;</span>


<span style="color: #999999">import org.springframework.stereotype.Controller;</span>


<span style="color: #999999">import org.springframework.web.bind.annotation.*;</span>


<span style="color: #999999">import org.springframework.web.multipart.MultipartFile;</span>





<span style="color: #993300">@Controller</span>


<span style="color: #003366"><span style="color: #993366">public class</span> SpringFileUploadController {</span>





<span style="color: #003366"><span style="color: #993300">  @PostMapping</span>("<span style="color: #000000">/upload</span>") </span>


<span style="color: #003366"><span style="color: #993366">  public</span> ResponseEntity<?> handleFileUpload( <span style="color: #993300">@RequestParam</span>("<span style="color: #000000">file</span>") MultipartFile file ) {</span>





<span style="color: #003366">    String fileName = file.getOriginalFilename();</span>


<span style="color: #003366"><span style="color: #0000ff">    try</span> {</span>


<span style="color: #003366">      file.transferTo( <span style="color: #0000ff">new</span> File("<span style="color: #000000">C:\\upload\\</span>" + fileName));</span>


<span style="color: #003366">    } <span style="color: #0000ff">catch</span> (Exception e) {</span>


<span style="color: #003366">      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();</span>


<span style="color: #003366">    } </span>


<span style="color: #003366"><span style="color: #0000ff">    return</span> ResponseEntity.ok("<span style="color: #000000">File uploaded successfully.</span>");</span>


<span style="color: #003366">  }</span>





<span style="color: #003366">}


</span>

The controller code assumes an Ajax-based invocation, so only status codes are returned to the browser. This simplifies the Spring MVC code, which means we don’t have to deal with forwards or results pages. We have one Spring Boot file upload controller on the server, and one Ajax-based HTML5 file uploader on the client.

More file upload options

I put together a bunch of file upload tutorials. Pick your technology and get uploading!

  • Want client and server-side JavaScript? Upload files with Node.js
  • Jakarta EE compliant Servlet and JSP file uploading
  • Why not upload files with Apache Commons?
  • Does anyone still use Struts to upload files?
  • Upload files with Ajax and JavaScript on the client
  • We even have a PHP file upload example

Uploading files to the server need not be a problem.

Ajax and Spring file uploader

The easiest way to add file upload functionality to a web page is to use the HTML5 file input tags. A developer triggers the form submission through an Ajax call, which eliminates the need to wrap the HTML5 file input field with form tags. That also introduces a few extra lines of JavaScript code, but the payoffs — user friendliness, and no need to create any other HTML pages — are worth it.

Developers should save the following code to a file named uploader.html in the templates directory of the Spring Boot file upload example project.

<span style="color: #999999"><html xmlns:th="http://www.thymeleaf.org"></span>


<span style="color: #999999"><head> </span>


<span style="color: #999999"><title> Ajax Spring File Upload Example </title> </span>


<span style="color: #999999"></head> </span>


<span style="color: #999999"><body>


</span>


<span style="color: #008000"><!-- HTML5 Input Form Elements --></span>


<span style="color: #003366"><input <span style="color: #993366">id</span>="fileupload" <span style="color: #993366">type</span>="file" <span style="color: #993366">name</span>="fileupload" /> </span>


<span style="color: #003366"><button <span style="color: #993366">id</span>="upload-button" <span style="color: #993366">onclick</span>="uploadFile()"> <span style="color: #000000">Upload</span> </button></span>





<span style="color: #008000"><!-- Ajax JavaScript File Upload to Spring Boot Logic --></span>


<span style="color: #000000"><script></span>


<span style="color: #003366"><span style="color: #0000ff">async function</span> uploadFile() {</span>


<span style="color: #003366"><span style="color: #0000ff">  let</span> formData = <span style="color: #0000ff">new</span> FormData(); </span>


<span style="color: #003366">  formData.append(<span style="color: #000000">"file"</span>, fileupload.files[0]);</span>


<span style="color: #003366"><span style="color: #0000ff">  let</span> response = <span style="color: #0000ff">await</span> fetch(<span style="color: #000000">'/upload'</span>, {</span>


<span style="color: #003366">    method: <span style="color: #000000">"POST"</span>, </span>


<span style="color: #003366">    body: formData</span>


<span style="color: #003366">  }); </span>





<span style="color: #003366"><span style="color: #0000ff">  if</span> (response.status == 200) {</span>


<span style="color: #003366">    alert(<span style="color: #000000">"File successfully uploaded."</span>);</span>


<span style="color: #003366">  }</span>


<span style="color: #003366">}</span>


<span style="color: #000000"></script></span>





<span style="color: #999999"></body> </span>


<span style="color: #999999"></html>


</span>

Run the Spring MVC file uploader

With the Spring controller coded in Java and the HTML file saved to the templates directory, a developer can deploy and test the application. When the application runs, the URL to bring up the HTML5 file upload form in the browser is:

http://localhost:8080/index

The result is an Ajax-based, Java Spring Boot file upload that persistently saves files uploaded to the server from the client’s browser.

PHP file upload example

The Spring Boot file upload controller will persistently save images and other files sent to the server from a the client’s browser.

The source code for this Spring file upload example is available GitHub.


Cameron McKenzie

Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.


Cameron McKenzie

Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.