春季上传文件大小限制错误

Rya*_*ing 6 java spring tomcat spring-boot spring-restcontroller

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

我到处寻找很多地方来尝试找到此错误的答案。我查看了所有这些问题,并尝试实施它们的建议,但无济于事:Spring上传文件大小限制我试图设置maxFileSize,但不兑现org.apache.tomcat.util.http.fileupload.FileUploadBase $ 春季启动时FileSizeLimitExceededExceptionMultipartFile的最大限制

我正在使用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);
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的所有application.properties配置:

1个

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
Run Code Online (Sandbox Code Playgroud)

2

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
Run Code Online (Sandbox Code Playgroud)

3

spring.http.multipart.max-file-size=5MB
spring.http.multipart.max-request-size=5MB
Run Code Online (Sandbox Code Playgroud)

4

multipart.max-file-size=5MB
multipart.max-request-size=5MB
Run Code Online (Sandbox Code Playgroud)

5

server.tomcat.max-file-size=5000000
Run Code Online (Sandbox Code Playgroud)

6

server.tomcat.max-http-post-size=5000000
Run Code Online (Sandbox Code Playgroud)

7

spring.servlet.multipart.maxFileSize=-1
spring.servlet.multipart.maxRequestSize=-1
Run Code Online (Sandbox Code Playgroud)

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

spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB
Run Code Online (Sandbox Code Playgroud)

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

vik*_*ngh 13

添加以下行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
Run Code Online (Sandbox Code Playgroud)

请注意较低的版本,即1.5.9.RELEASE以及下面的配置,如下所示:

spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB
Run Code Online (Sandbox Code Playgroud)


Kni*_*der 0

这就是我所拥有的,它非常适合我的后端。

@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;
    }
Run Code Online (Sandbox Code Playgroud)

在 bootstrap.yml 文件中,我只会有类似这样的内容...它只允许最大文件大小为 2 MB。

---
spring:
  servlet:
    multipart:
      max-file-size: 2MB
      max-request-size: 2MB
Run Code Online (Sandbox Code Playgroud)

最后,在我的 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>
Run Code Online (Sandbox Code Playgroud)

这应该工作得很好。我也在使用 Spring Boot 2.0.3