How to upload files with Spring MVC

In this tutorial, we discuss how to upload files with Spring MVC.

If you’re more interested in doing it the old way using Servlets, then check “How to upload files to a Servlet” tutorial.

1- Upload.html

Before digging into the controller, let’s see how would the HTML form looks like:

The above is a very simple HTML form which only holds an input file along with a submit button.

As noticed, we set the encoding type to “multipart/form-data” and define “/upload” API to handle the request.

2- Controller

In order to parse the uploaded files at the server-side, you have to create a controller method that maps the “/upload” requests, the method should:

  1. Consume “multipart/form-data” request type.
  2. Has an input argument of type MultipartFile[].

The below method parses the uploaded files and saves them to the file system:

As noticed, we parse the uploaded file using getInputStream() method and we get its name through getOriginalFilename() method.

P.S: If you feel confused on the above try/catch syntax, check “Try with resources” tutorial.

0 0 vote
Article Rating

Hussein Terek

Founder of, I have a passion in software engineering and everything related to java environment.

You may also like...

Inline Feedbacks
View all comments