Spring Boot修改最大上传文件限制:The field file exceeds its maximum permitted size of 1048576 bytes.

SpringBoot做文件上传时出现了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
multipart.maxRequestSize

Spring Boot 1.3.x或者之前

multipart.maxFileSize=100Mb
multipart.maxRequestSize=1000Mb

Spring Boot 1.4.x

spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=1000Mb

很多人设置了multipart.maxFileSize但是不起作用,是因为1.4版本以上的配置改了,详见官方文档:spring boot 1.4

  Spring Boot 2.0之后

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=1000MB

  • 71
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
FileUploadBase$FileSizeLimitExceededException是一个异常,它表示上传的文件大小超过了允许的最大大小。在这个特定的异常中,上传的文件大小超过了1048576字节(1MB)。 该异常通常会与文件上传相关的框架或库一起使用,如Apache Tomcat的文件上传组件或Spring框架的Multipart组件。当文件上传的大小超过配置的最大限制时,这个异常会被抛出。 在Spring框架中,你可以通过设置spring.http.multipart.maxFileSize属性来配置允许的最大文件大小。默认情况下,这个属性被设置为10MB。如果要修改最大文件大小,你可以在配置文件中设置该属性的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.](https://blog.csdn.net/weixin_64061088/article/details/129297518)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring boot上传附件报错:org.apache.tomcat.util.http.fileupload.FileUploadBase$...](https://blog.csdn.net/fxj0720/article/details/80255651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值