Saradha
Saradha

Reputation: 157

Spring boot 2.2.0 - Multipart File upload failing with error

I am trying to do file upload API in my spring boot application.Below is my controller method

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

        return certificateRequest;
    }

I have configured multipart properties in the application.properties file like below

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

In request Header, I have set the Content-Type property as multipart/form-data

But I am getting the following error while uploading file

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]

Not sure what else I am missing. while debugging I also found that

context.getAllowCasualMultipartParsing() is coming as false .Looks like multipart configuration are not set in the tomcat context .I am using embedded tomcat .

Upvotes: 0

Views: 6451

Answers (2)

Saradha
Saradha

Reputation: 157

I have fixed the issue . In my springApplication class MultipartAutoConfiguration was excluded

@SpringBootApplication(exclude = {MultipartAutoConfiguration.class})

Fixed it by removing "MultipartAutoConfiguration.class" from the exclusion list

Upvotes: 3

Ganesh Gudghe
Ganesh Gudghe

Reputation: 1387

you can use the following configuration based on version

Spring Boot 1.3.x and earlier

  multipart.maxFileSize
   multipart.maxRequestSize

Spring Boot 1.4.x and 1.5.x

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

Spring Boot 2.x

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

Currently, you are using 2.x then you can change your config to

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

Upvotes: 1

Related Questions