1. 程式人生 > >Spring Boot修改最大上傳檔案限制:The field file exceeds its maximum permitted size of 1048576 bytes.

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 
版權宣告:本文為博主原創文章,轉載請附上博文連結!