Java Spring上传文件大小限制错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51410540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring upload file size limit error
提问by Ryan Fasching
I am using Spring Boot and I can send images that are smaller than 1MB, but when I make a post request with a large image larger than 1MB I get this error:
我正在使用 Spring Boot,我可以发送小于 1MB 的图像,但是当我使用大于 1MB 的大图像进行发布请求时,我收到此错误:
Maximum upload size exceeded; nested exception is java.lang.IllegalStateException:org.apache.tomcat.util.
http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
I have been looking around so many places to try to find the answer to this error. I have looked at all of these questions and tried implementing what they suggest to no avail: Spring upload file size limit, I am trying to set maxFileSize but it is not honored, org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException, and Max Limit of MultipartFile in spring boot
我已经环顾了很多地方,试图找到这个错误的答案。我已经查看了所有这些问题,并尝试实施他们的建议无济于事:Spring 上传文件大小限制, 我正在尝试设置 maxFileSize 但它没有得到尊重,org.apache.tomcat.util.http.fileupload.FileUploadBase$ FileSizeLimitExceededException和Spring Boot 中 MultipartFile 的最大限制
I am using version 2.0.3 of Spring and here is my post mapping:
我使用的是 Spring 2.0.3 版,这是我的帖子映射:
@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
String message = "";
try {
storageService.store(file);
files.add(file.getOriginalFilename());
message = "You successfully uploaded " + file.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.OK).body(message);
} catch (Exception e) {
message = "FAIL to upload " + file.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
}
}
And here are all the application.properties configuration that I have tried:
这是我尝试过的所有 application.properties 配置:
1
1
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
2
2
spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
3
3
spring.http.multipart.max-file-size=5MB
spring.http.multipart.max-request-size=5MB
4
4
multipart.max-file-size=5MB
multipart.max-request-size=5MB
5
5
server.tomcat.max-file-size=5000000
6
6
server.tomcat.max-http-post-size=5000000
7
7
spring.servlet.multipart.maxFileSize=-1
spring.servlet.multipart.maxRequestSize=-1
Even tried changing it to application.yml:
甚至尝试将其更改为 application.yml:
spring:
servlet:
multipart:
max-file-size: 5MB
max-request-size: 5MB
I also looked into changing the request size allowed by Tomcat in web.xml file, but I don't have a web.xml file. The Tomcat I am using is bundled in to the app.
我还研究了在 web.xml 文件中更改 Tomcat 允许的请求大小,但我没有 web.xml 文件。我正在使用的 Tomcat 捆绑在应用程序中。
回答by Knight Rider
This is what I have and it works perfectly for me for the backend.
这就是我所拥有的,它非常适合我的后端。
@PutMapping(value = USER_PROFILE_UPDATE_URL_MAPPING)
public String updateProfile(
@ModelAttribute(AUTHORIZED_USER_MODEL_KEY) User user,
BindingResult bindingResult,
@RequestParam(name = "file") MultipartFile multipartFile,
Model model, @RequestParam Map<String, String> params) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User authorizedUser = (User) authentication.getPrincipal();
//....
return REDIRECT_TO_LOGIN;
}
And in the bootstrap.yml file, I will simply have something like this... which will only allow the maximum file size of 2 megabytes.
而在 bootstrap.yml 文件中,我只会有这样的东西......它只允许最大文件大小为 2 兆字节。
---
spring:
servlet:
multipart:
max-file-size: 2MB
max-request-size: 2MB
and finally, in my HTML file, I will have something like this...
最后,在我的 HTML 文件中,我会有这样的东西......
<form id="profileForm"
th:action="@{/update}"
th:object="${user}"
method="post" enctype="multipart/form-data">
<!-- This will modify the method to PUT to match backend -->
<input type="hidden" name="_method" value="PUT">
...
<div class="col-md-3">
<div class="form-group row">
<label for="file" class="custom-file">
<input name="file" type="file" id="file" aria-describedby="fileHelpId" class="form-control-file btn btn-outline-info">
</label>
<small id="fileHelpId" class="form-text text-muted">Maximum size should be 2MB</small>
</div>
</div>
And this should work perfectly fine. I am also using Spring Boot 2.0.3
这应该工作得很好。我也在使用 Spring Boot 2.0.3
回答by T.Rex
Did you try this ?
你试过这个吗?
spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize = 20MB
OR
或者
spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB
Works for me with Spring Boot 1.5.x
使用 Spring Boot 1.5.x 对我有用
回答by kj007
As per latest Spring Boot Common propertiesbelow should work
根据最新的Spring Boot Common 属性,下面应该可以使用
MULTIPART (MultipartProperties)
MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
or If you just want to control the multipart properties then multipart.max-file-size
and multipart.max-request-size
properties should work.
或者如果你只是想控制多属性然后multipart.max-file-size
和multipart.max-request-size
性能应该工作。
multipart.max-file-size=5MB
multipart.max-request-size=5MB
回答by vikash singh
Add the below lines in application.propertiesfor Spring Boot version 2.0.0.RELEASE and above-
添加以下行application.properties春季引导版本2.0.0.RELEASE及以上-
spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.servlet.multipart.enabled=true
Please note for lower versions, i.e. 1.5.9.RELEASE and belowthe configuration used to be as follows:
请注意较低版本,即1.5.9.RELEASE 及以下版本的配置如下:
spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB