Springboot 文件上传超过限制处理
2018-10-16 本文已影响39人
织梦少年666
springboot文件上传报错
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileName exceeds its maximum permitted size of 1048576 bytes.
Spring Boot默认文件上传大小为2M,多文档上传中总是出现文件大小超出限度
处理
1.在application.properties文件中设置文件大小
# Single file max size
multipart.maxFileSize=50Mb
# All files max size
multipart.maxRequestSize=100Mb
2.在启动类App.class文件中配置Bean来设置文件大小
package com.source;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
@SpringBootApplication
@Configuration
public class SpringbootUploadDownloadApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootUploadDownloadApplication.class, args);
}
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("50240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("1002400KB");
return factory.createMultipartConfig();
}
}