Spring Boot无法上传大于1MB文件:遇FileSizeLimitExceededException异常
Got it, let's tackle that org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException you're seeing. This happens because Spring Boot sets a default maximum file upload size of 1MB out of the box. Here's how to adjust this based on your Spring Boot version:
For Spring Boot 2.0+ (including 3.x)
The easiest way is to update your application configuration file (either application.properties or application.yml):
Using application.properties
# Maximum size for a single uploaded file spring.servlet.multipart.max-file-size=10MB # Maximum total size for all files in a single request spring.servlet.multipart.max-request-size=20MB
Using application.yml
spring: servlet: multipart: max-file-size: 10MB max-request-size: 20MB
You can use units like KB, MB, or GB depending on your needs—just make sure the unit is uppercase (e.g., MB not mb).
For Spring Boot 1.x
The configuration prefix is slightly different here:
Using application.properties
spring.http.multipart.max-file-size=10MB spring.http.multipart.max-request-size=20MB
Using application.yml
spring: http: multipart: max-file-size: 10MB max-request-size: 20MB
Alternative: Programmatic Configuration
If you need more flexibility (like setting a custom temp file location), you can create a configuration class:
import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.MultipartConfigElement; @Configuration public class FileUploadConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // Set max size per file factory.setMaxFileSize("10MB"); // Set max total size for all files in one request factory.setMaxRequestSize("20MB"); // Optional: Set a custom temp directory to avoid permission issues factory.setLocation(System.getProperty("java.io.tmpdir")); return factory.createMultipartConfig(); } }
Quick Notes
- If you're using a non-Tomcat container (like Jetty or Undertow), these configurations still work—Spring Boot's auto-config handles the container-specific adjustments.
- If you still hit limits after updating, check if there's an upstream proxy (like Nginx) or frontend code enforcing smaller file sizes.
内容的提问来源于stack exchange,提问作者Shiva Kumar N

