转自:https://blog.csdn.net/shi0299/article/details/69525848
springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一种是加个Bean。
第一种:
application.properties中添加
spring.http.multipart.maxFileSize=10Mbspring.http.multipart.maxRequestSize=10Mb
maxFileSize 是单个文件大小,
maxRequestSize是设置总上传的数据大小,
这就可以了。根据自己需求定义吧,Mb和Kb都可以,大小写也都随意,Long字节应该可以的
另外这是MultipartConfigFactory类中的转换,可以瞅一眼
private long parseSize(String size) {Assert.hasLength(size, "Size must not be empty");size = size.toUpperCase();return size.endsWith("KB")?Long.valueOf(size.substring(0, size.length() - 2)).longValue() * 1024L:(size.endsWith("MB")?Long.valueOf(size.substring(0, size.length() - 2)).longValue() * 1024L * 1024L:Long.valueOf(size).longValue());}
第二种:
增加Bean配置,注意当前类上需要加注解@Configuration,不然扫不到就不会起作用了。
/*** 文件上传配置* @return*/@Beanpublic MultipartConfigElement multipartConfigElement() {MultipartConfigFactory factory = new MultipartConfigFactory();//文件最大factory.setMaxFileSize("10240KB"); //KB,MB/// 设置总上传数据总大小factory.setMaxRequestSize("102400KB");return factory.createMultipartConfig();}
一般配置放在启动类中就可以。
import javax.servlet.MultipartConfigElement;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.MultipartConfigFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })public class Application {protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);}public static void main(String[] args) {try {SpringApplication.run(Application.class, args);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Beanpublic MultipartConfigElement multipartConfigElement() {MultipartConfigFactory factory = new MultipartConfigFactory();//文件最大factory.setMaxFileSize("1024MB"); //KB,MB//设置总上传数据总大小factory.setMaxRequestSize("102400MB");return factory.createMultipartConfig();}}
原文地址:https://www.cnblogs.com/zhangwuji/p/9152578.html
时间: 2024-09-28 16:00:31