Spring MVC Multiple File Upload Validation Example

This tutorial shows you about Spring MVC Multiple File Upload Validation Example. This example uses Spring validator to validate the uploaded file. This tutorial extends from the tutorial Spring MVC File Upload Validation Example.

Talbe of contents:
1. Maven Dependencies
2. Project Structure
3. Spring MVC File Upload Java/XML Configuration
4. Spring MVC File Upload
5. Upload file views
6. Deploy Spring MVC Multiple File Upload Validation Example

Other interesting posts you may like

Now, we are ready to build the Spring MVC Multiple File Upload Validation Example step by step

Maven Dependencies
We added the commons-fileupload which is used to upload a MultipartFile. We also include the javax.validation API so we can annotate our controller methods with the @Valid annotation.

Project Structure
The Project structure will looks like the following structure.

Spring Mvc Multiple File Upload Validation Example

Spring MVC File Upload Java/XML Configuration
The CommonsMultipartResolver saves temporary files to the temporary directory in application server. The following properties should be configured for the file upload operation: maxUploadSize, maxUploadSizePerFile, maxInMemorySize, uploadDir, defaultEncoding and resolveLazily. Although they are optionally but you need to set the property resolveLazily to true if you want to catch execption correctly.

If you want to adapt the file upload feature into your current Spring MVC XML configuration. You can add the below configuration. It is equivalent with the above java configuration.

For simplicity, the navigation cases will be put in a NavigationController.

Validation Messages
These validation messages are used to display an appropriate message with an error operation to the user. These messags are stored in the property file messages.properties that is located in the src/main/resources/ folder.

Spring MVC Multi File Upload
The MultiFileModel is used to upload multiple files. We reuse FileModel of the previous post.

The MultiFileUploadController processes multiple file uploads.

The MultipleFileValidator handles validates multiple files. It’ll reject the request that submit a empty file.

Upload file views
We created a simple view to upload multiple files.

Upload view

Success View

When the file is successfully uploaded, the file name will displayed

Error View

The content of the error is displayed if an error occurs.

Deploy Spring MVC Multipe File Upload Validation Example
Building project with maven then deploy file war on application server or servlet container (Tomcat 8 for example). Access the address URL http://localhost:8080/spring-mvc-multiple-file-upload/ and browse files to upload such as the below screen shot.

Spring Mvc Multiple File Upload Validation Example upload

Click the Upload button the success screen will display

Spring Mvc Multiple File Upload Validation Example upload success

Click the Upload Page link to return the Upload form screen and submit form without browsing file. The error screen will display such as

Spring Mvc Multiple File Upload Validation Example upload error

That’s it on how to build Spring MVC Multiple File Upload Validation Example.

Download complete source code of example, please click link below

Spring-Mvc-Multiple-File-Upload-Validation-Example.zip (51 downloads)

Source code on Github https://github.com/javabycode/spring-mvc-multiple-file-upload-validation-example

Leave a Comment

*

Please share it if you found this useful
Hide Buttons