Java : How to handle file size exceed exception of tomcat

标签 java spring tomcat exception-handling multipart

我正在处理 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/

相关文章:

java - 使用选择排序方法对二维数组进行排序并将结果写入文件

java - 在 Spring 中指定相对于上下文 XML 的位置

java - Spring Java 配置上下文的事务配置

jsf - 无法在 Tomcat 7 中初始化 FacesServlet - ClassNotFoundException

java - 部署在 Wildfly 14 中有效,但在 15 中无效。 IllegalArgumentException 不能同时要求和提供相同的依赖项

java - 如何在没有 Slick 的情况下从 PNG 加载 LWJGL 纹理?

java - 通用套接字代理服务器也可以有效地与保持 Activity 连接一起工作(Java)

java.lang.IllegalArgumentException : Cannot find a factory to create the request context

java - 我无法访问 Apache Tomcat 管理器?

java - 为什么我的网络应用程序在 tomcat/bin 而不是在 webapps 中寻找所需的文件?