springboot文件上传时maxPostSize设置大小失效问题及解决
springboot文件上传时maxPostSize设置大小失效
报错信息
Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector
Caused by: java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector
该配置尝试无效,百度说是版本问题,核对过后发现无误
servlet: multipart: enabled: true max-file-size: 1000MB max-request-size: 1000MB
解决办法
因为我这里上传是传图片,图片以base64形式携带在请求参数中,form表单的形式提交,故怀疑可能是请求参数大小被限制了,于是添加以下配置
#注意这是server!!不是上面的servlet,别看错了。。。 server: tomcat: max-http-post-size: 100MB #请求参数长度 max-http-form-post-size: 100MB #form表单长度
重启解决
springboot设置文件上传大小限制
问题
SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
解决方法
第一种解决方案:
增加Bean配置,注意当前类上需要加注解@Configuration,不然扫不到就不会起作用了;一般配置放在启动类中就可以。
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}第二种解决方案:
在配置文件(application.properties/application.yml)中加入如下设置即可:
# 单个文件大小(Mb和Kb都可以) spring.servlet.multipart.maxFileSize=30MB # 总上传的数据大小 spring.servlet.multipart.maxRequestSize=30MB
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据的具体实现
ThreadPoolTaskExecutor是Spring提供的任务执行器实现之一,允许开发者配置线程池参数以适应不同的应用场景,创建 ThreadPoolTaskExecutor 实例并设置核心和最大线程数等属性可以优化性能,本文介绍了SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据的具体实现2024-12-12
springboot日期格式化全局LocalDateTime详解
文章主要分析了Spring Boot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spring Boot的自动配置类JacksonAutoConfiguration,文章详细说明了ObjectMapper对象的创建和配置过程2025-02-02


最新评论