【发布时间】:2023-04-01 18:01:01
【问题描述】:
上传大于设定值的文件时出现错误。我想要
捕获此异常并将其返回给浏览器。
e.getCause().getMessage() 有值,但没有成功返回
浏览器。通常,浏览器上会显示一段 json。
handleFileFormatException 没问题,但是handleIllegalStateException 无法显示任何信息,在浏览器上出现“无法访问此站点”。路径都显示:localhost:8080。这两个方法几乎一样,不同的是FileFormatExceptio是我定义的异常类,而IllegalStateException不是。为什么它没有将 json 实体返回给浏览器。我不知道该怎么办。谁能帮帮我?谢谢!
应用程序属性:
@ExceptionHandler
异常响应
public class ExceptionResponse {
private String message;
private Integer code;
public ExceptionResponse(Integer code, String message){
this.message = message;
this.code = code;
}
public static ExceptionResponse create(Integer code, String message){
return new ExceptionResponse(code, message);
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
控制台警告,没有错误
2017-07-31 17:10:50.388 WARN 10940 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (42990730) exceeds the configured maximum (10485760)
控制器
@Controller
public class FileUploadController {
private final StorageService storageService;
@Autowired
public FileUploadController(StorageService storageService) {
this.storageService = storageService;
}
@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {
model.addAttribute("files", storageService
.loadAll()
.map(path ->
MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
.build().toString())
.collect(Collectors.toList()));
return "index";
}
@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
Resource file = storageService.loadAsResource(filename);
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
.body(file);
}
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes){
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"upload success! " + file.getOriginalFilename() + "!");
return "redirect:/";
}
getStatus
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
【问题讨论】:
-
尝试捕捉
org.springframework.web.multipart.MultipartException而不是java.lang.IllegalStateException。 -
@ResponseBody @ExceptionHandler(MultipartException.class) public String handleSizeExceededException(HttpServletRequest request,MultipartException ex) { return ex.getMessage(); }
-
但是浏览器仍然没有显示响应错误
-
哦,我不明白是什么问题,现在我明白了。你能给我们看看
ExceptionResponse的课程吗?您能否使用浏览器中的开发工具检查并告诉我们该应用程序真正返回的内容是什么。 (状态、标题、内容) -
好的!我更新了一些你需要的信息,你可以看看
标签: java spring-mvc exception spring-boot exceptionhandler