Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes
错误信息:
The field file exceeds its maximum permitted size of 1048576 bytes
原因:
原因是因为SpringBoot内嵌tomcat默认所能上传的文件大小为1M,超出这个就会报错。
解决方案:
一、在配置文件中修改springboot的默认配置
spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: 30MB
二、编写配置类
package com.blog.springboot.config; import javax.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MulterFile { /** * 文件上传配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大 factory.setMaxFileSize("30960KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("309600KB"); return factory.createMultipartConfig(); } }
上面的两种解决方案,有利也有弊。第一种更加简洁,方便不需要使用多余的代码。第二种的话,虽然需要写更多的代码,还需要增加一个配置文件,但是把这种配置信息摆在明面上要更加好一些,因为,这样有利于后期的维护,并且,这种配置一般情况下是很少有变更的,基本上配置一次就ok。减少了yml文件中的冗余信息。尤其是针对springcloud项目来说。
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/articles/14984075.html

浙公网安备 33010602011771号