SpringBoot上传文件大小限制的配置

解决SpingBoot框架上传文件时,如果文件大小超过了1MB报错的bug

1
2
Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
The field file exceeds its maximum permitted size of 1048576 bytes 

  原因是SpringBoot内置的Tomcat的文件传输默认单个文件最大1M,单次请求文件总数大小为10M。
解决方法:
可以在SpingBoot的application.yml配置文件中进行修改

  SpingBoot2.0版本之前:

1
2
3
4
5
spring:
  http:
    multipart:
      maxFileSize: 20MB  #单个文件最大为20M
      maxRequestSize: 20MB #单次请求文件总数大小为20M 

  SpingBoot2.0版本之后:

1
2
3
4
5
spring:
  servlet:
    multipart:
      max-file-size: 20MB #单个文件最大为20M
      max-request-size: 20MB #单次请求文件总数大小为20M