The field file exceeds its maximum permitted size of 1048576 bytes.

简介: The field file exceeds its maximum permitted size of 1048576 bytes.

@[toc]

一、背景描述

本项目是个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,搞定!!!

完结!

相关文章
|
13天前
|
Docker 容器
devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 163840 free dat
devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 163840 free dat
8 0
|
6月前
|
Java 应用服务中间件
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
63 0
|
8月前
|
Java Spring
The field file exceeds its maximum permitted size of 1048576 bytes 文件上传大小受到限制,报错!!
The field file exceeds its maximum permitted size of 1048576 bytes 文件上传大小受到限制,报错!!
94 0
|
前端开发 Java 关系型数据库
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
1036 0