springboot上传文件时报错The field file exceeds its maximum permitted size of 1048576 bytes.

谁践踏了优雅 2023-02-10 03:40 55阅读 0赞

我所使用的springboot版本如下

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.2.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </parent>

第一步,在配置文件中增加如下

  1. spring:
  2. servlet:
  3. multipart: null
  4. enabled: true
  5. max-file-size: 10mb
  6. max-request-size: 10mb

【经过百度,发现网上的小伙伴们都说加了这段代码就可以,但是在我的项目中还是依旧报错,所以才有下面的第二步】

第二步,在启动类中加入代码

  1. /**
  2. * Description: springboot对文件上传大小控制
  3. *
  4. * @interfaceName
  5. * @return
  6. */
  7. @Bean
  8. public MultipartConfigElement multipartConfigElement() {
  9. MultipartConfigFactory factory = new MultipartConfigFactory();
  10. // 单个数据大小
  11. factory.setMaxFileSize(DataSize.ofMegabytes(10)); // MB
  12. // 总上传数据大小
  13. factory.setMaxRequestSize(DataSize.ofMegabytes(100));
  14. return factory.createMultipartConfig();
  15. }

通过以上两步,问题已解决

发表评论

表情:
评论列表 (有 0 条评论,55人围观)

还没有评论,来说两句吧...

相关阅读