使用springboot上传文件,当文件超过2M报出异常

异常显示org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2325669) exceeds the configured maximum (2097152)

请求被拒绝,因为其大小(2325669)超过了配置的最大值(2097152),可见对于上传文件的请求有个默认配置最大值是2M

于是我们上传1M多的文件时,结果报出异常

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

字段mfile超出其最大允许大小1048576字节,可见对于MultipartFile文件也有限制,默认限制是1M

于是我们上传小于1M文件,这次终于成功了,但是我们平时上传文件不可能只是小于1M或2M的,还需要修改这个配置

怎么去修改呢

不同版本,对应的设置值不一样

Spring Boot 1.3.x and earlier

  • multipart.maxFileSize
  • multipart.maxRequestSize

Spring Boot 1.4.x and 1.5.x

  • spring.http.multipart.maxFileSize
  • spring.http.multipart.maxRequestSize

Spring Boot 2.x

  • spring.servlet.multipart.maxFileSize
  • spring.servlet.multipart.maxRequestSize

比如在2.0设置

spring.servlet.multipart.maxFileSize=30MB

spring.servlet.multipart.maxRequestSize=30M
不做限制
spring.servlet.multipart.maxFileSize=-1

spring.servlet.multipart.maxRequestSize=-1

另一种方法

写一个config类

import javax.servlet.MultipartConfigElement;import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;@Configuration
public class TomcatConfig {@Value("${spring.http.server.maxFileSize}")private Long maxFileSize;@Value("${spring.http.server.maxRequestSize}")private Long maxRequestSize;@Beanpublic MultipartConfigElement multipartConfigElement() {MultipartConfigFactory factory = new MultipartConfigFactory();// 单个数据大小// factory.setMaxFileSize(MaxFileSize); // KB,MBfactory.setMaxFileSize(DataSize.ofMegabytes(maxFileSize));/// 总上传数据大小factory.setMaxRequestSize(DataSize.ofMegabytes(maxRequestSize));// factory.setMaxRequestSize(MaxRequestSize);return factory.createMultipartConfig();}
}

然后在application.properties文件中配置

spring.http.server.maxFileSize和spring.http.server.maxRequestSize的值,效果是一样的

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nes相关推荐

  1. 解决org.springframework.web.multipart.MaxUploadSizeExceededException报错问题

    在springboot中接收到上传的文件时候,出现了这种错误 org.springframework.web.multipart.MaxUploadSizeExceededException:  Ma ...

  2. 解决:org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request;

    项目场景: 微服务修改保存,出现系统异常 问题描述 报错日志如下: | 2022-10-24 11:32:42.882 |-[1;31mERROR[m [http-nio-9000-exec-4] [ ...

  3. Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()

    转自:https://blog.csdn.net/l1028386804/article/details/65449355 ava.lang.NoSuchMethodException: [org.s ...

  4. 普元 AppServer 6.5 将springboot应用部署到应用服务器,上传文件时报错:Caused by: org.springframework.web.multipart.Multipar

    [问题] springboot应用部署到应用服务器后,上传文件时报错: Caused by: org.springframework.web.multipart.MultipartException: ...

  5. org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart

    SpringMVC文件上传报错: java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be ...

  6. 图片上传问题 org.springframework.web.multipart.MultipartException

    背景 测试环境图片上传是正常的,但测试环境异常 [ERROR]{"level":"ERROR","time":"2022-09-2 ...

  7. Springboot Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException

    Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required req ...

  8. org.springframework.web.multipart.MultipartException: Current request is not a multipart request

    在做项目时,后端采用上传文件组件MultipartFile时,使用swagger测试文件上传出现了以下问题: 2022-03-24 22:02:07 |ERROR |http-nio-9000-exe ...

  9. Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.init()

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/65449355 java.lang.NoSuchMethodException: [ ...

最新文章

  1. iptables连接跟踪ip_conntrack
  2. 入门代码教程第二节 如何:实现服务协定
  3. python读取.so_Python入门-编译安装Python3教程
  4. poj3273---Monthly Expense
  5. 16 SD配置-企业结构-分配-给信贷控制区分配公司代码
  6. 【Kafka】kafka zk下注册的信息 get /brokers/ids/1 “host“:null “port“:-1
  7. python 人脸检测_借助摄像头在Python中实现人脸检测
  8. 2019-06-04 Sublime Text 中文输入法的问题
  9. X64_Xcelera-CL_PX4采集卡测试记录
  10. UIWebView背景透明的方法
  11. 深度学习——卷积神经网络(CNN)
  12. RestTemplate总结
  13. 25~30K的国外企业招聘面试考题,10分钟就能写出来了?
  14. 保持健康和活力 - 颈椎病康复指南
  15. js根据时间戳获取日期失败,NaN-NaN-NaN NaN:NaN:NaN
  16. 爬取网易buff CSGO饰品数据
  17. 使用IO口检测电源电压的转换逻辑
  18. NVT平台:G-sensor(DA380)驱动调试
  19. 关于Github上zheng项目部署问题总结
  20. SRT上传文件下载文件

热门文章

  1. 尚品汇前台(学习笔记)
  2. 保险业务与系统——保险上云解决方案总体架构及应用案例
  3. html 不要忘记结束标签
  4. 【Java源码分析】String 检测 startsWith
  5. 垂直森林被列入全球50座最具标志性的摩天大楼之一
  6. for循环语句与while循环语句的结构与用法
  7. 注册后 域名服务器,域名和服务器有什么区别?注册域名后需要购买服务器吗?...
  8. 根据天数(由用户输入)计算这些天数中有几周和剩余的天数,例如:用户输入的是45 ,输出结果为:45天是6周余3天
  9. 企企通携手塑料新材料行业龙头【佛塑科技】,以数字化采购引领行业转型升级
  10. excel打印预览在哪里_打印小心机|不会打印页眉页脚你就输了