Spring Boot修改最大上传文件限制:The field file exceeds its maximum permitted size of 1048576 bytes.

SpringBoot做文件上传时出现了The field file exceeds its maximum permitted size of 1048576 bytes.错误,显示文件的大小超出了允许的范围。查看了官方文档,原来Spring Boot工程嵌入的tomcat限制了请求的文件大小,这一点在Spring Boot的官方文档中有说明,原文如下

65.5 Handling Multipart File Uploads
Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1Mb per file and a maximum of 10Mb of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the multipart.maxFileSize property to -1.The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.

文档说明表示,每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。要更改这个默认值需要在配置文件(如dapplication.properties)中加入两个配置

Spring Boot 1.3.x或者之前

multipart.maxFileSize=100Mb
multipart.maxRequestSize=1000Mb


Spring Boot 1.4.x

spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=1000Mb


Spring Boot 2.0之后

spring.servlet.multipart.max-file-size=100Mb
spring.servlet.multipart.max-request-size=1000Mb

第二种方法

在启动类里面加入以下代码

@Bean
	public MultipartConfigElement multipartConfigElement() {
	  MultipartConfigFactory factory = new MultipartConfigFactory();
	  //单个文件最大
	  factory.setMaxFileSize("5MB");
	  /// 设置总上传数据总大小
	  factory.setMaxRequestSize("100MB");
	  return factory.createMultipartConfig();
	}

需要设置以下两个参数

multipart.maxFileSize
multipart.maxRequestSize
--------------------- 
作者:Chopper_Tony 
来源:CSDN 
原文:https://blog.csdn.net/awmw74520/article/details/70230591 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_29072049/article/details/83583888