Spring boot MVC file upload with Exception Handling


This small project i created has the capability to create a post, upload a file and etc. But today i'm only interested in file upload and file upload exception handling.

We will be adding validations to check the files type and file Size.

If you want to see the code straightly without the other crap, please go to github, be my guest  : https://github.com/h-hub/java_cms

For this project we need and uses
  1. Java 8 or above.
  2. Spring boot (MVC)
  3. Thymeleaf
  4. Hibernate/ JPA
  5. Bootstrap/css/html
  6. Tomcat (embedded)
  7. Maven
  8. Mysql (Database dump included in the github repo)
This project is a Spring boot 2.0 project and i find spring boot is very easy to set up and develop. Thanks pivotal for making my life easy. For those who haven't tried Spring boot yet : please try  http://spring.io/projects/spring-boot

Check the below code segments from controller and service

BlogController.java

This line is responsible for uploading the file
imagePath = blogPostService.createImage( (new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date())) + file.getOriginalFilename(), file, BLOG_IMAGES_DIR_ABSOLUTE_PATH);

It calls the createImage method in the service layer. check below code.

BlogPostServiceImpl.java

That's all for the image upload code. There are several other method in these java files. here we are focusing only on the image upload. We use DTO objects to transfer data from front end to the controllers.

Adding Configurations


In Spring Boot, you can’t configure the embedded Tomcat maxSwallowSize via the common application properties. So you have to add below code to configure your application

[snippet slug=webmvcconfig lang=java]

But if you are using stand-alone tomcat, then change maxSwallowSize property in your server.xml file like below

<Connector port="8089" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" 
    maxSwallowSize="-1"/>

Here we are setting the upload size to unlimited. Next, you have to add below configuration to the application.properties

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB

This configuration limits file size to 5MB.

Next i will add custom validations to limit the files types which can be uploaded. Check below two classes which implements the validations

Then we can add the annotations to the class like this
@ContentType({"image/jpg","image/jpeg","image/png","image/gif"})
public class BlogDto { ......

Adding the Exception Handler


Here we are using @ControllerAdvice to handle the exceptions.  If the user tries to upload an image larger than 5MB it will catch the MultipartException and redirect to "/add_blog_post" url and display the error message

In this post i have only included the code segments and go to github to see the full project.  https://github.com/h-hub/java_cms

Few screen shots of the UI







It is a maven project and easy to set up with eclipse.

References:

Comments