org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded;


springboot默認設置的上傳文件的大小是1MB。

 

springboot中的源碼:

@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
public class MultipartProperties {

    /**
     * Whether to enable support of multipart uploads.
     */
    private boolean enabled = true;

    /**
     * Intermediate location of uploaded files.
     */
    private String location;

####### 在這里設置了上傳文件的大小#######
/** * Max file size. */ private DataSize maxFileSize = DataSize.ofMegabytes(1); /** * Max request size. */ private DataSize maxRequestSize = DataSize.ofMegabytes(10); /** * Threshold after which files are written to disk. */ private DataSize fileSizeThreshold = DataSize.ofBytes(0); /** * Whether to resolve the multipart request lazily at the time of file or parameter * access. */ private boolean resolveLazily = false; public boolean getEnabled() { return this.enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public String getLocation() { return this.location; } public void setLocation(String location) { this.location = location; } public DataSize getMaxFileSize() { return this.maxFileSize; } public void setMaxFileSize(DataSize maxFileSize) { this.maxFileSize = maxFileSize; } public DataSize getMaxRequestSize() { return this.maxRequestSize; } public void setMaxRequestSize(DataSize maxRequestSize) { this.maxRequestSize = maxRequestSize; } public DataSize getFileSizeThreshold() { return this.fileSizeThreshold; } public void setFileSizeThreshold(DataSize fileSizeThreshold) { this.fileSizeThreshold = fileSizeThreshold; } public boolean isResolveLazily() { return this.resolveLazily; } public void setResolveLazily(boolean resolveLazily) { this.resolveLazily = resolveLazily; } /** * Create a new {@link MultipartConfigElement} using the properties. * @return a new {@link MultipartConfigElement} configured using there properties */ public MultipartConfigElement createMultipartConfig() { MultipartConfigFactory factory = new MultipartConfigFactory(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold); map.from(this.location).whenHasText().to(factory::setLocation); map.from(this.maxRequestSize).to(factory::setMaxRequestSize); map.from(this.maxFileSize).to(factory::setMaxFileSize); return factory.createMultipartConfig(); } }
    /**
     * Obtain a {@link DataSize} representing the specified number of megabytes.
     * @param megabytes the number of megabytes, positive or negative
     * @return a {@link DataSize}
     */
    public static DataSize ofMegabytes(long megabytes) {
        return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
    }

所以可以看出來,在Springboot中設置的上傳的文件的大小默認是1MB。

由兩種方法可以進行設置上傳文件的大小:

第一種就是在配置文件中配置最大的上傳值:

Spring Boot 1.3.x and earlier

  • multipart.maxFileSize
  • multipart.maxRequestSize

Spring Boot 1.4.x and 1.5.x

  • spring.http.multipart.maxFileSize
  • spring.http.multipart.maxRequestSize

Spring Boot 2.x

  • spring.servlet.multipart.maxFileSize
  • spring.servlet.multipart.maxRequestSize

比如在2.0設置

  • spring.servlet.multipart.maxFileSize=30MB
  • spring.servlet.multipart.maxRequestSize=30M

不做限制

  • spring.servlet.multipart.maxFileSize=-1
  • spring.servlet.multipart.maxRequestSize=-1

另一種方法

   寫一個config類

import javax.servlet.MultipartConfigElement;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
 
@Configuration
public class TomcatConfig {
 
    @Value("${spring.http.server.maxFileSize}")
    private Long maxFileSize;
    @Value("${spring.http.server.maxRequestSize}")
    private Long maxRequestSize;
 
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 單個數據大小
        // factory.setMaxFileSize(MaxFileSize); // KB,MB
        factory.setMaxFileSize(DataSize.ofMegabytes(maxFileSize));
        /// 總上傳數據大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(maxRequestSize));
        // factory.setMaxRequestSize(MaxRequestSize);
        return factory.createMultipartConfig();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM