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.
@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;
  }
}
 @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