Spring Boot 2.2.0-多部分文件上传失败并显示错误

时间:2019-12-02 12:02:16

标签: java spring spring-boot

我正在尝试在Spring Boot应用程序中执行文件上传API。以下是我的控制器方法

    public CertificateRequest uploadfileAndParse(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        CertificateRequest certificateRequest = new CertificateRequest();
        if(!file.isEmpty()){
            certificateRequest.setCity("UN");
        }

        return certificateRequest;
    }

我已经在application.properties文件中配置了多部分属性,如下所示

spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=2KB
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=215MB

在请求标题中,我已将Content-Type属性设置为multipart / form-data

但是上传文件时出现以下错误

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided] with root cause
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been providedat org.apache.catalina.connector.Request.parseParts(Request.java:2802) ~[tomcat-embed-core-9.0.27.jar:9.0.27]

不确定我还缺少什么。 在调试时,我还发现

context.getAllowCasualMultipartParsing()作为false出现。在tomcat上下文中未设置类似multipart的配置。我正在使用嵌入式tomcat。

2 个答案:

答案 0 :(得分:0)

您可以根据版本使用以下配置

Spring Boot 1.3.x和更早版本

  multipart.maxFileSize
   multipart.maxRequestSize

Spring Boot 1.4.x和1.5.x

spring.http.multipart.maxFileSize
spring.http.multipart.maxRequestSize

Spring Boot 2.x

spring.servlet.multipart.maxFileSize
spring.servlet.multipart.maxRequestSize

当前,您正在使用2.x  然后您可以将配置更改为

spring.servlet.multipart.maxFileSize
spring.servlet.multipart.maxRequestSize

答案 1 :(得分:0)

我已经解决了这个问题。在我的springApplication类中,MultipartAutoConfiguration被排除了

@SpringBootApplication(exclude = {MultipartAutoConfiguration.class})

通过从排除列表中删除“ MultipartAutoConfiguration.class”来修复该问题