簡體   English   中英

Spring上傳文件大小限制錯誤

[英]Spring upload file size limit 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.

我一直在尋找很多地方試圖找到這個錯誤的答案。 我已經查看了所有這些問題並嘗試實施他們的建議無濟於事: Spring upload file size limit我正在嘗試設置 maxFileSize 但不尊重org.apache.tomcat.util.http.fileupload.FileUploadBase$ Spring Boot 中的FileSizeLimitExceededExceptionMultipartFile 的 Max Limit

我使用的是 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);
  }

}

這是我嘗試過的所有 application.properties 配置:

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 捆綁在應用程序中。

application.properties中為 Spring 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

根據下面最新的Spring Boot Common 屬性應該可以工作

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.max-file-sizemultipart.max-request-size屬性應該可以工作。

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

取決於 Spring boot 版本,例如 1.X,在您的 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

這就是我所擁有的,它對我來說非常適合后端。

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

這應該工作得很好。 我也在使用 Spring Boot 2.0.3

你試過這個嗎?

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

或者

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

使用 Spring Boot 1.5.x 為我工作

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

參考org.springframework.boot.autoconfigure.web.MutltipartProperties類來了解設置屬性時使用什么前綴。 上述所有命名約定適用於不同版本的 springframework。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM