Java 关于Springboot上载文件大小的问题
上载大于设置值的文件时出错。我想 捕获此异常并将其返回到浏览器Java 关于Springboot上载文件大小的问题,java,spring-mvc,exception,spring-boot,exceptionhandler,Java,Spring Mvc,Exception,Spring Boot,Exceptionhandler,上载大于设置值的文件时出错。我想 捕获此异常并将其返回到浏览器 e.getCause().getMessage()具有值,但未成功返回到 浏览器通常,浏览器上将显示一段json。 handleFileFormatException没有问题,但是handleAllegalStateException无法显示任何消息,并且在浏览器上出现“无法访问此站点”。路径都显示为:localhost:8080。这两种方法几乎相同,不同之处在于FileFormatExceptio是我定义的异常类,而Illegal
e.getCause().getMessage()
具有值,但未成功返回到
浏览器通常,浏览器上将显示一段json。
handleFileFormatException
没有问题,但是handleAllegalStateException
无法显示任何消息,并且在浏览器上出现“无法访问此站点”。路径都显示为:localhost:8080。这两种方法几乎相同,不同之处在于FileFormatExceptio
是我定义的异常类,而IllegalStateException
不是。为什么它不将json实体返回到浏览器。我不知道该怎么办,谁能帮我?谢谢大家!
application.properties:
@例外处理程序
例外响应
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)
浏览器按F12
控制器
@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:/";
}
您缺少在异常处理程序处理异常时设置http响应状态代码的部分
- 将
添加到您的方法中李>@ResponseStatus(value=HttpStatus.BAD_REQUEST)
- 将
添加到方法参数列表中,并调用HttpServletResponse
response.setStatus(HttpStatus.BAD_REQUEST.value())返回对象之前,请执行代码>李>
- 将
添加到您的方法中李>@ResponseStatus(value=HttpStatus.BAD_REQUEST)
- 将
添加到方法参数列表中,并调用HttpServletResponse
response.setStatus(HttpStatus.BAD_REQUEST.value())返回对象之前,请执行代码>李>
org.springframework.web.multipart.MultipartException
而不是java.lang.IllegalStateException
@ResponseBody@ExceptionHandler(MultipartException.class)公共字符串handleSizeExcepedException(HttpServletRequest,MultipartException-ex){return ex.getMessage();}但是浏览器仍然没有显示响应错误。哦,我不知道问题出在哪里,现在我明白了。您能给我们看一下类异常响应?你能使用浏览器中的开发工具检查一下吗?告诉我们应用程序真正返回的是什么。(状态、标题、内容)好!我已经更新了一些您需要的信息,您可以查看一下,尝试捕获org.springframework.web.multipart.MultipartException
而不是java.lang.IllegalStateException
@responseBy@ExceptionHandler(MultipartException.class)公共字符串handleSizeExcepedException(HttpServletRequest,MultipartException-ex){return-ex.getMessage();}但是浏览器仍然没有显示响应错误。哦,我不知道问题出在哪里,现在我知道了。你能给我们看一下类异常响应
?你能从浏览器中使用开发工具检查一下吗?告诉我们应用程序真正返回的是什么。(状态、标题、内容)好的!我已经更新了一些您需要的信息,您可以查看一下我已经有了它,“HttpStatus status=getStatus(request);”但是昨天它没有返回任何东西,今天也可以返回相同的代码,我不知道为什么,太神奇了!所以你的解释也是正确的,我会接受,谢谢!我不知道你的代码中的方法getStatus
在做什么。但是你试图从请求中提取http状态代码(我认为这不会返回任何内容)。您需要做的是更新HttpServletResponse
(响应而不是请求)http状态代码值,以真正影响应用程序发送回浏览器的http响应。好的,我更新了我的getStatus()方法。虽然我没有完全理解,但是程序现在没有问题,所以我觉得这个方法应该也没有问题。你可以看看。我已经有了它,“HttpStatus status=getStatus(request);”但是昨天它没有返回任何东西,今天也可以返回相同的代码,我不知道为什么,太神奇了!所以你的解释也是正确的,我会接受,谢谢!我不知道你的代码中的方法getStatus
在做什么。但是你试图从请求中提取http状态代码(我认为这不会返回任何内容)。您需要做的是更新HttpServletResponse
(响应而不是请求)http状态代码值,以真正影响应用程序发送回浏览器的http响应。好的,我更新了我的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);
}