首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot 2.x版本+MultipartFile设置指定文件上传大小

SpringBoot 2.x版本+MultipartFile设置指定文件上传大小

作者头像
Arebirth
发布2020-06-19 15:51:35
发布2020-06-19 15:51:35
5.1K0
举报

SpringBoot-versio:2.1.9-RELEASE

由于新版本的SpringBoot已经弃用了(1.5版本支持)如下,

这种方式,提供了新的 配置方案。

这个是官方的介绍

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.

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.

方案一:

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

代码语言:javascript
复制
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB

方案二:

  编写配置类,并通过@Bean标签来加入到IOC容器中管理
代码语言:javascript
复制
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));
        //设置总上传数据大小10GB
        factory.setMaxRequestSize(DataSize.ofGigabytes(10L));

        return factory.createMultipartConfig();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot-versio:2.1.9-RELEASE
    • 这个是官方的介绍
  • 方案一:
    •   application.properties配置(yml一样,只是格式有变化)
  • 方案二:
    •   编写配置类,并通过@Bean标签来加入到IOC容器中管理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档