Spring MVC - Wie wird die maximale Upload-Größe überschritten?

Spring MVC - Der Umgang mit der maximalen Upload-Größe hat die Ausnahme überschritten

Im Frühjahr können Sie@ControllerAdvice deklarieren, um die hässliche maximale Upload-Größe zu erfassen, die die Ausnahme wie folgt überschreitet:

spring-file-upload-max-file-size

Lösung

Hängt von den Arten vonmultipartResolver ab:

  1. StandardServletMultipartResolver -MultipartException fangen, siehethis example.

  2. CommonsMultipartResolver - catchMaxUploadSizeExceededException - beziehen sich aufthis example.

GlobalExceptionHandler.java

package com.example.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@ControllerAdvice
public class GlobalExceptionHandler {

    //StandardServletMultipartResolver
    @ExceptionHandler(MultipartException.class)
    public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";

    }

    //CommonsMultipartResolver
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public String handleError2(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";

    }

}

Tomcat Connection Reset
Wenn Sie eine Bereitstellung für Tomcat durchgeführt haben und die Dateigröße nicht abfangen konnten, kann dies durch die Einstellung von TomcatmaxSwallowSizeverursacht werden. Lesen Sie dies -Spring file upload and connection reset issue