Spring Boot 2中spring.servlet.multipart.max-file-size最大值与无限配置疑问
Hey there, let's break down your multipart upload issue step by step.
Your Core Questions & Problem Recap
First, let's address your initial questions clearly:
- What's the maximum value for
spring.servlet.multipart.max-file-sizein Spring Boot 2? There's no hard-coded upper limit—it's ultimately constrained only by your server's memory and disk storage capacity. - Can you set it to unlimited? Yes, in theory setting
-1should disable the limit, but your experience shows there's more to it, which we'll dive into.
You mentioned that when you configured:
spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1
it didn't work—uploading files larger than 10MB threw an error about the request exceeding 10485760 bytes (10MB), same as when you had no config at all. But when you set max-request-size=1024MB, large files uploaded successfully.
Why This Happened
Let's unpack the root causes behind the behavior you saw:
1. You Hit the Default max-request-size Limit
Spring Boot's out-of-the-box defaults are:
max-file-size: 1MB (single file limit)max-request-size: 10MB (total size of the entire request, including all files and form data)
When you set max-file-size=-1, you removed the single-file limit, but max-request-size was still stuck at the default 10MB. Any file larger than 10MB would make the entire request exceed this limit, triggering the error you encountered.
2. Undertow's Hidden Default Limit
The error message UT000020: Connection terminated as request was larger than 10485760 tells us you're using Undertow as your embedded container. Undertow has its own default max-http-post-size set to 10MB, which can override Spring Boot's multipart config if not adjusted. Even if you set Spring's max-request-size=-1, Undertow's built-in limit would still block large requests.
Fixes to Get True Unlimited Uploads
Here's how to resolve this completely:
1. Ensure Both Multipart Parameters Are Set to -1
First, confirm your application.properties has both parameters set to disable limits:
spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1
Double-check the file is in src/main/resources (the default path) or that your active profile is loading the correct config file if you're using multi-environment setups.
2. Override Undertow's Post Size Limit
Add this line to your application.properties to disable Undertow's built-in request size limit:
server.undertow.max-http-post-size=-1
If you prefer YAML configuration:
server: undertow: max-http-post-size: -1
3. Verify Your Config Is Applied
To make sure your settings are taking effect, you can add a simple controller to expose the current multipart config:
@RestController public class MultipartConfigChecker { @Autowired private MultipartProperties multipartProps; @GetMapping("/multipart/config") public MultipartProperties getCurrentConfig() { return multipartProps; } }
Hit this endpoint—you should see maxFileSize and maxRequestSize set to -1 if everything's working correctly.
Final Notes
- Setting
-1for both Spring multipart parameters and Undertow's post size will effectively remove all upload size limits (though you'll still be constrained by your server's hardware capabilities). - Your earlier partial fix (
max-request-size=1024MB) worked because it raised the request size limit above your file's size, but it's not a true "unlimited" solution.
内容的提问来源于stack exchange,提问作者Arijeet Bhakat




