gpt4 book ai didi

Java : How to handle file size exceed exception of tomcat

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:28 25 4
gpt4 key购买 nike

我正在处理 spring boot 项目,我想在其中处理最大文件大小超过异常(我只想上传那些大小小于 10MB 的文件,如果有人尝试上传大于 10MB 的文件,它应该返回一个消息也不异常(exception)),在互联网上搜索后,我尝试了所有可能的解决方案,但没有任何效果

谁能告诉我这件事?

FileUploadController.class

@ControllerAdvice
@Controller
public class FileUploadController extends ResponseEntityExceptionHandler{
@ExceptionHandler(MaxUploadSizeExceededException.class)
@RequestMapping(method = RequestMethod.POST, value = "/uploadChildPhoto/{childId}", produces = "application/json")
public @ResponseBody ResponseEntity<?> uploadChildPhoto(Authentication authentication,
@PathVariable("childId") Long childId, @RequestParam("file") MultipartFile file,MaxUploadSizeExceededException exc) {
try {
if (!file.isEmpty()) {
ChildPhoto createdPhoto = childService.createChildPhoto(file, childId);
return ResponseEntity.ok(createdPhoto);
} else {
throw new RuntimeException(
"You failed to upload " + file.getOriginalFilename() + " because the file was empty");
}
} catch (MultipartException ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("File size error");
}
}
}

异常

org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14816520) exceeds the configured maximum (10485760)
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:805) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.Request.parseParts(Request.java:2864) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.Request.parseParameters(Request.java:3211) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.Request.getParameter(Request.java:1137) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23]

最大文件大小

spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB

最佳答案

由于 tomcat 抛出异常,您无法处理它。因此,您需要告诉 tomcat 允许上传所有大小的文件并在 Controller 级别处理大小。

在下面设置多部分属性多部分:最大文件大小:-1 最大请求大小:-1

设置 Tomcat 8 (maxSwallowSize="-1")在 Controller 上,添加检查大小的逻辑

if(fileAttachment.getSize() > 10485760 ) { throw new MaxUploadSizeExceededException(fileAttachment.getSize()); }

关于Java : How to handle file size exceed exception of tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48820561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com