[JAVA] Error handling when the maximum file size is exceeded when uploading a file with Spring Boot

You can set the maximum file size when uploading a file with Spring Boot. Set the following parameters in application.propertes

spring.servlet.multipart.enabled=true
#Maximum size of one file
spring.servlet.multipart.max-file-size=10MB
#Maximum size of all multiple files
spring.servlet.multipart.max-request-size=50MB

An error will be displayed if you try to upload a file larger than the size set here, but an example when you want to customize this error handling is described below.

  1. Create child class of HandlerExceptionResolver Create the following class anywhere in the project. This makes it possible to get an exception for file size error.
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
  @Override
  public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
    ModelAndView modelAndView = new ModelAndView();
    if (e instanceof MultipartException && e.getCause() instanceof IllegalStateException && e.getCause().getCause() instanceof FileSizeLimitExceededException) {
      //Messages you want to display, etc.
      modelAndView.addObject("message", "File size exceeded");
    }
    //Specify the screen you want to transition to
    modelAndView.setViewName("error");
    return modelAndView;
  }
}
  1. Setting Max Swallow Size of built-in tomcat Set MaxSwallowSize of built-in Tomcat to -1 because 1. alone does not work well. The following methods are described in XXXXApplication.java where the main method is located.
 @Bean
  public TomcatServletWebServerFactory containerFactory() {
    return new TomcatServletWebServerFactory() {
      protected void customizeConnector(Connector connector) {
        super.customizeConnector(connector);
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {

          ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
      }
    };
  }

Recommended Posts

Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
What is a Spring Boot .original file?
[Note] Configuration file when using Logback with Spring Boot
A memorandum when creating a REST service with Spring Boot
File upload with Spring Boot
A story packed with the basics of Spring Boot (solved)
About the behavior when doing a file map with java
I want to control the maximum file size in file upload for each URL in Spring Boot
When a Java file created with the Atom editor is garbled when executed at the command prompt
wsimport error handling (A class / interface with the same name "xxx" is already in use)
I get a 404 error when testing forms authentication with Spring Security
Read the file under the classpath as a character string with spring
Create a Spring Boot app development project with the cURL + tar command
Create a jar file with the command
Javaw.exe error when starting Spring Boot (STS)
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
[Spring framework] HTTP request error handling by RestTemplate when using a proxy server
Resource handler settings when delivering SPA with the static resource function of Spring Boot
Starting with Spring Boot 2.3, the default error page no longer contains detailed error information
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple search app with Spring Boot
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
Error handling when Gradle dependency is using SLF4J
Create a web api server with spring boot
[Spring Boot] How to refer to the property file
When I call the file with Class # getResource from the jar file, it becomes Not Found and it is a crappy memorandum
Create a Spring Boot development environment with docker
What I was addicted to when developing a Spring Boot application with VS Code
A story that stumbled when deploying a web application created with Spring Boot to EC2
Customize the display when an error such as 404 Not Found occurs in Spring Boot
Form and process file and String data at the same time with Spring Boot + Java
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
[Spring] Read a message from a YAML file with MessageSource
Access the built-in h2db of spring boot with jdbcTemplate
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Create a simple demo site with Spring Security with Spring Boot 2.1
Customize REST API error response with Spring Boot (Part 1)
I wrote a test with Spring Boot + JUnit 5 now
File upload with Spring Boot (do not use Multipart File)
[Rails] What to do when the view collapses when a message is displayed with the errors method