SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制

1,905 阅读1分钟

SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制

前言

LZ最近升级SpringBoo框架到2.1.6,踩了一些坑,这里介绍的是文件上传大小限制。

升级前
  #文件上传配置 1.5.9
   spring:
       http:
          multipart:
              enabled: true
              max-file-size: 100Mb
              max-request-size:100Mb
升级后
  ##文件上传配置 2.x
   spring:
     servlet:
       multipart:
         enabled: true
         max-file-size: 100Mb
         max-request-size: 100Mb
原因

我们可以从源码分析,找到SpringBoot的相关源码——MultipartProperties


package org.springframework.boot.autoconfigure.web.servlet;

import javax.servlet.MultipartConfigElement;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.util.StringUtils;

@ConfigurationProperties(
    prefix = "spring.servlet.multipart",
    ignoreUnknownFields = false
)
public class MultipartProperties {
    private boolean enabled = true;
    private String location;
    private String maxFileSize = "1MB";
    private String maxRequestSize = "10MB";
    private String fileSizeThreshold = "0";
    private boolean resolveLazily = false;
    .........
}

上面是SpringBoot2.x源码,从上面可以看出,maxFileSize,即最大文件大小,默认被限制为1MB,maxRequestSize即最大请求大小,默认被限制为10MB。该类的注解中prefix=spring.servlet.multipart。