Spring MVC File Upload Example

Spring MVC File upload is made easy by the Apache Commons FileUpload dependency. Lets see the steps on how we can use that in the sample code for Spring MVC File Upload.

1. Dependencies in pom.xml

Add the following dependencies of Apache Commons in your pom.xml

2. MultipartResolver configuration

Create bean of the CommonsMultipartResolver in your dispacther-servlet.xml

3. Form to get the File

Create a View to get the file from User and then submit it to the Controller.
The important point to note is that form enctype should be multipart/form-data, so that Spring web application knows that the request to the controller contains file data.

4. File Upload Controller

Add a Controller to process the file that was sent using enctype=”multipart/form-data”
Please note that the param to the uploadFile method is a MultipartFile .

5. Success page

Finally after you are done with processing of the file show a success or error page.

Output

Spring MVC File Upload Example

Download Full Code – SpringMVC-SingleFileUpload.zip

References

1. Spring Uploading Files

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn