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

Spring Boot做文件上传时出现了The field file exceeds its maximum permitted size of 1048576 bytes.错误,显示文件的大小超出了允许的范围。查看了官方文档,原来Spring Boot工程嵌入的tomcat限制了请求的文件大小,这一点在Spring Boot的官方文档中有说明,原文如下

65.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 file of 1Mb per file and a maximum of 10Mb of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the multipart.maxFileSize 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.

文档说明表示,每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。要更改这个默认值需要在配置文件(如application.properties)中加入两个配置

multipart.maxFileSize = 10Mb
multipart.maxRequestSize=100Mb

multipart.maxFileSize=10Mb是设置单个文件的大小, multipart.maxRequestSize=100Mb是设置单次请求的文件的总大小

如果是想要不限制文件上传的大小,那么就把两个值都设置为-1就行啦

Spring Boot1.4版本后配置更改为:

spring.http.multipart.maxFileSize = 1Mb  
spring.http.multipart.maxRequestSize=10Mb  

Spring Boot2.0之后的版本配置修改为:

spring.servlet.multipart.max-file-size = 1MB  
spring.servlet.multipart.max-request-size=10MB

详情参考Spring Boot官方文档


如果以上的配置有误,尝试将=更改为:或者将Mb单位更改为MB,以Spring Boot的官方文档为准

Spring Boot各版本文档地址(比较齐全)

https://docs.spring.io/spring-boot/docs/

目录:reference/html/

转自:https://blog.csdn.net/u010429286/article/details/54381705

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值