tomcat Spring上载文件大小限制错误

8wtpewkr  于 2022-11-13  发布在  Spring
关注(0)|答案(7)|浏览(198)

我使用的是Sping Boot ,可以发送小于1MB的图像,但当我使用大于1MB的大图像发出post请求时,出现以下错误:

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.

我已经找了很多地方试图找到这个错误的答案。我已经看了所有这些问题,并试图实施他们建议的无济于事:Spring upload file size limitI am trying to set maxFileSize but it is not honoredorg.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededExceptionMax Limit of MultipartFile in spring boot中的一个或多个
我使用的是Spring的2.0.3版本,下面是我的帖子Map:

@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);
  }

}

下面是application.properties我尝试过的所有www.example.com配置:
1

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

2

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB

3

spring.http.multipart.max-file-size=5MB
spring.http.multipart.max-request-size=5MB

4

multipart.max-file-size=5MB
multipart.max-request-size=5MB

5

server.tomcat.max-file-size=5000000

6

server.tomcat.max-http-post-size=5000000

7

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

甚至尝试将其更改为application.yml:

spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB

我还考虑过在web.xml文件中更改Tomcat允许的请求大小,但我没有web.xml文件。我使用的Tomcat是捆绑在应用程序中的。

zujrkrfu

zujrkrfu1#

application.properties中为Sping Boot 版本2.0.0.RELEASE及更高版本添加以下行-

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.servlet.multipart.enabled=true

请注意,对于较低版本,即1.5.9.RELEASE及更低版本,配置通常如下所示:

spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB
ruoxqz4g

ruoxqz4g2#

根据最新的Spring Boot Common properties,以下应工作

MULTIPART(多部分属性)

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.

或者如果您只想控制multipart属性,则multipart.max-file-sizemultipart.max-request-size属性应该可以使用。

multipart.max-file-size=5MB
multipart.max-request-size=5MB
lstz6jyr

lstz6jyr3#

依赖于Sping Boot 版本,例如1.X,在您的www.example.com上application.properties设置文件大小限制:

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

Spring Boot 2.x /以上版本:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
xj3cbfub

xj3cbfub4#

这是我所拥有的,它对我的后端工作得很完美。

@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;
    }

而在bootstrap.yml文件中,我只会有这样的东西...这将只允许最大文件大小为2兆字节。

---
spring:
  servlet:
    multipart:
      max-file-size: 2MB
      max-request-size: 2MB

最后,在我的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>

这应该工作得很好。我也在使用Sping Boot 2. 0. 3

ukxgm1gy

ukxgm1gy5#

你试过这个吗?

spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize = 20MB

spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB

适用于Sping Boot 1.5.x

rkue9o1l

rkue9o1l6#

This worked for me.
We can make changes to file size either in
1. application.properties 2. TOMCAT configuration(server.xml)

A. Ideally we should make use of application.properties 
B. And for that first we need to disable in TOMCAT config file "server.xml" by setting maxSwallowSize=-1 .

    <Connector port="8080" protocol="HTTP/1.1"
      connectionTimeout="20000"
      redirectPort="8443"
      maxSwallowSize = "-1"/>

C. Then set application.properties as

spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB
spring.servlet.multipart.enabled=true
wwtsj6pe

wwtsj6pe7#

请参考org.springframework.boot.autoconfigure.web.MutltipartProperties类来了解在设置属性时使用什么前缀。以上所有命名约定适用于不同版本的springframework。

相关问题