【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

简介: 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

一、背景描述

本项目是个Springboot 项目,功能是要做一个文件上传,在测试时发现报错,上传的是一个 word 文件,大小是 1.25MB,报错内容如下:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

详细报错内容如下图所示:

二、错误原因

SpringBoot 项目在文件上传时出现了 Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 的错误,显示文件的大小超出了允许的范围。原因是 SpringBoot内嵌的 tomcat 默认的所有上传的文件大小为 1MB,超出这个大小就会报错,解决这个问题需要更改以下两个默认。

  • multipart.maxFileSize
  • multipart.maxRequestSize

三、解决方案

解决方案呢,要根据SpringBoot的版本来确定,以下列举出了几种情况,可以根据自己项目的SpringBoot版本来对应:把默认的文件的大小限制修改一下即可。以下是我项目中的用到的文件大小限制,改成50MB即可满足我的需求了。

3.1 SpringBoot 1.3.x 之前

multipart.maxFileSize=50Mb
multipart.maxRequestSize=50Mb

3.2 SpringBoot 1.4.x

spring.http.multipart.maxFileSize=50Mb
spring.http.multipart.maxRequestSize=50Mb

3.3 SpringBoot 2.0.x之后

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

配置完上述限制之后,重启项目,Bingo,搞定!!!

完结!


相关文章
|
3月前
|
JSON PHP 数据格式
PHP Allowed memory size of 134217728 bytes exhausted (tried to allocate 10489856 bytes)
PHP Allowed memory size of 134217728 bytes exhausted (tried to allocate 10489856 bytes)
64 2
|
5月前
|
区块链
max code size exceeded
max code size exceeded
59 6
|
Java Spring
The field file exceeds its maximum permitted size of 1048576 bytes 文件上传大小受到限制,报错!!
The field file exceeds its maximum permitted size of 1048576 bytes 文件上传大小受到限制,报错!!
657 0
|
Java 应用服务中间件
The field file exceeds its maximum permitted size of 1048576 bytes.
The field file exceeds its maximum permitted size of 1048576 bytes.
|
缓存 数据可视化
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
210 0
|
前端开发 Java 关系型数据库
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
1122 0