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
- Java 8 or above.
- Spring boot (MVC)
- Thymeleaf
- Hibernate/ JPA
- Bootstrap/css/html
- Tomcat (embedded)
- Maven
- Mysql (Database dump included in the github repo)
Check the below code segments from controller and service
BlogController.java
This line is responsible for uploading the fileimagePath = 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:
- https://www.journaldev.com/2651/spring-mvc-exception-handling-controlleradvice-exceptionhandler-handlerexceptionresolver
- https://www.journaldev.com/2651/spring-mvc-exception-handling-controlleradvice-exceptionhandler-handlerexceptionresolver
- https://stackoverflow.com/questions/45345839/springboot-application-properties-upload-file-exception
- https://www.mkyong.com/spring-boot/spring-boot-configure-maxswallowsize-in-embedded-tomcat/
- https://www.baeldung.com/web-mvc-configurer-adapter-deprecated
Comments
Post a Comment