SpringBoot 2.x 设置上传文件大小

JAVA学习网 2020-03-03 09:45:01

在使用SpringBoot进行文件上传时,出现 文件上传过大的问题。

原因是SpringBoot自带集成的Tomcat限制了文件上传大小。默认为1M.

解决方案如下:

方案一:

application.properties配置(yml一样,只是格式有变化)

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

方案二:

编写配置类,并通过@Bean标签来加入到IOC容器中管理

 

package com.xxx.config;


import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@Component
public class MultipartConfig {
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置单个文件上传大小
        factory.setMaxFileSize(DataSize.parse("1024MB"));
        // 设置总上传数据大小
        factory.setMaxRequestSize(DataSize.parse("1024MB"));
        return factory.createMultipartConfig();
    }

}

 

阅读(2133) 评论(0)