宇智波擎枫
2021-01-18 10:37@ControllerAdvice @ExceptionHandler 不能重定向页面
个人按照实例编写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条回答
为你推荐
- 路由返回了无效的JSON。 也许是一个例外被抛出?
- laravel
- jwt
- php
- 1个回答
- Lumen:异常处理程序 - 不捕获QueryException或PDOException
- exception
- laravel
- mysql
- php
- 1个回答
- Laravel - 致命错误:未捕获错误:未找到类'Auth'
- laravel
- php
- 3个回答
- 当我使用Monolog的ExceptionHandler时,如何显示异常?
- php
- 1个回答
- spring3 注解式事务不起作用
- spring
- hibernate
- 0个回答
换一换