SpringBoot 2.x version + MultipartFile setting specifies the file upload size

SpringBoot-versio:2.1.9-RELEASE

 

Since the new version of SpringBoot been abandoned (version 1.5 support) as follows,

 

 This way, a new configuration.

 

This is the official description

 Handling Multipart File Uploads

Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default, Spring Boot configures Spring MVC with a maximum size of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, the location to which intermediate data is stored (for example, to the /tmp directory), and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. For example, if you want to specify that files be unlimited, set the spring.servlet.multipart.max-file-size property to -1.

The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.

See the MultipartAutoConfiguration source for more details.

[Note]

It is recommended to use the container’s built-in support for multipart uploads rather than introducing an additional dependency such as Apache Commons File Upload.

 

 

Option One:

  application.properties configuration (yml the same, but there are changes in format)

spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB

 

Option II:

  Writing configuration class, and to add to the vessel IOC managed by tab @Bean

package cn.arebirth.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@Configuration
public class FileUploadConfiuration {
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
 //单个文件大小200mb
        factory.setMaxFileSize (DataSize.ofMegabytes ( 200L ));
         // set the total data to be uploaded 10GB 
        factory.setMaxRequestSize (DataSize.ofGigabytes ( 10L )); 

        return factory.createMultipartConfig (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/arebirth/p/springbootsetmultipartfilesize.html