[Solved] Spring Boot Error: The field file exceeds its maximum permitted size of 1048576 bytes

Error message:

The field file exceeds its maximum permitted size of 1048576 bytes

the reason:

The reason is that the file size that SpringBoot embedded tomcat can upload by default is 1M, and an error will be reported if it exceeds this.

solution:

1. Modify the default configuration of springboot in the configuration file

spring:
  http:
    multipart:
      enabled: true 
      max -file- size: 30MB
      max -request-size: 30MB

Two, write the configuration class

package com.blog.springboot.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;

@Configuration
public  class MulterFile {
     /**  
     * File upload configuration  
     * @return   
     */  
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();  
         // Maximum file size   
        factory.setMaxFileSize("30960KB"); // KB,MB   
        // / Set the total upload data size   
        factory.setMaxRequestSize("309600KB" );  
         return factory.createMultipartConfig() ;  
    }
}

The above two solutions have advantages and disadvantages. The first is more concise and convenient without using redundant code. In the second case, although more code needs to be written and a configuration file needs to be added, it is better to put this configuration information on the bright side, because this is beneficial to the later maintenance, and this configuration Under normal circumstances, there are very few changes, basically the configuration is ok once. Reduce the redundant information in the yml file. Especially for the springcloud project.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *