SpringBoot工程设置文件上传的大小

  • A+
所属分类:Spring

SpringBoot工程设置文件上传的大小

项目开发中遇到了上传文件时,提示上传文件大小为1M,这样满足不了项目的需求,需要上传大文件,上传时报如下错:

Maximum upload size exceeded
the request was rejected because its size (9739444) exceeds the configured maximum (5242880)

springboot默认上传文件大小为1M,此处超过限制。

如何调整上传的文件大小

首先,修改Springboot内置Tomcat的maxPostsize值,如下

//HTTP Post内容的最大大小(字节)
server.tomcat.max-http-post-size=-1

然后,在application.properties配置文件大小参数

//最大文件大小
multipart.maxFileSize=150MB
//最大请求大小
multipart.maxRequestSize=150MB

设置好,配置文件参数后,需要在启动主类中获取配置的参数值,如下:

@Bean
public MultipartConfigElement multipartConfigElement(@Value("${multipart.maxFileSize}") String maxFileSize, @Value("${multipart.maxRequestSize}") String maxRequestSize) {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(maxFileSize);
        factory.setMaxRequestSize(maxRequestSize);
        return factory.createMultipartConfig();
    }

启动应用后,再次上传文件就不会出现超出限制的错误了。

  • 我的微信
  • 加好友一起交流!
  • weinxin
  • 微信公众号
  • 关注公众号获取分享资源!
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: