sweetysxj
宇智波擎枫
2021-01-18 10:37

@ControllerAdvice @ExceptionHandler 不能重定向页面

  • spring
  • java
  • tomcat
  • maven
  • intellij-idea

个人按照实例编写Spring Boot Upload File页面,很简单的操作

1、上传页面

<body>
    <h1>File Upload</h1>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <br><br>
        <input type="submit" value="Submit">
    </form>
</body>

2、处理方法

@RequestMapping("/upload")
    public String singleFileUpload(@RequestParam("file")MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:/uploadStatus";
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
            redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/uploadStatus";
    }

@RequestMapping("/uploadStatus")
    public String uploadStatus() {
        return "uploadStatus";
    }

3、GlobalExceptionHandler

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MultipartException.class)
    public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";
    }
}

实例运行中,正常上传文件没有问题,在超过预定设置大小后,控制台报错

2021-01-18 10:24:27.730  WARN 2248 --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (77299500) exceeds the configured maximum (52428800)]
2021-01-18 10:24:27.771  WARN 2248 --- [nio-8080-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (77299500) exceeds the configured maximum (52428800)]
2021-01-18 10:24:27.797  WARN 2248 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (77299500) exceeds the configured maximum (52428800)]

不知道为什么是3条?

全局异常GlobalExceptionHandler中的方法也捕捉到异常,但是不会执行重定向return "redirect:/uploadStatus";

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答