Springboot 文件上傳超過限制處理


springboot文件上傳報錯

一、錯誤原因

springboot項目在上傳較大文件時報錯: 
Maximum upload size exceeded;org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

報錯的原因是:

2、在啟動類上加上@CONFIGURATION注解,且在類中添加一下代碼:

 

二、解決方法

知道了是tomcat的默認設置限制了上傳的文件大小,那我們只需要改變默認設置即可。

1、在【APPLICATION.PROPERTIES】配置文件中加入如下代碼:

  1.  
    # maxFileSize 單個數據大小
  2.  
    spring.servlet.multipart.maxFileSize = 10MB
  3.  
    # maxRequestSize 是總數據大小
  4.  
    spring.servlet.multipart.maxRequestSize=100MB

有兩點要注意:

(1)這里“10MB”不能寫成“10Mb”,否則會報另一個錯,如下:

(2)SpringBoot的版本不同,這兩個配置語句也不一樣,具體版本對應如下:

  • Spring Boot 1.3 版本:

     multipart.maxFileSize

  • Spring Boot 1.4 版本和 1.5 版本:

     spring.http.multipart.maxFileSize

  • Spring Boot 2.0 版本:

     spring.servlet.multipart.maxFileSize

 

2、在配置類上加上@CONFIGURATION注解 配置Bean,且在類中添加一下代碼:

/**
 * @Description:
 * @Auther: MingHao
 * @CreateDate: 14:13 2020-4-8
 * @Version: 1.0
 */
@Configuration
public class MultipartConfig {
    /**
     * 文件上傳配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 單個數據大小
        factory.setMaxFileSize("102400KB"); // KB,MB
        // 總上傳數據大小
        factory.setMaxRequestSize("1024000KB");
        return factory.createMultipartConfig();
    }
}

 

 

3、如有網關,在網關中也加入如上配置(外面進不來,里面設置了也沒用)

 

 

4.以上設置完以后,需要配置日志信息

  g4j:WARN No appenders could be found for logger (com.netease.qa.testng.TestngRetry).
  log4j:WARN Please initialize the log4j system properly.

 

文件的名稱為  log4j.properties  , 文件中的內容設置為:

log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

 

 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM