为什么这个最大文件大小配置不起作用?

Why this max file size configuration not working?

我将这行添加到我的 application.properties 文件中:

# Multipart Configuration
multipart.maxFileSize = 150Mb
multipart.maxRequestSize = 150Mb
multipart.fileSizeThreshold = 5Mb

但是当我尝试上传大于 1MB 的文件时,出现此错误:

2019-11-20 22:18:06.802 ERROR 154240 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field bytes exceeds its maximum permitted size of 1048576 bytes.] with root cause

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field bytes exceeds its maximum permitted size of 1048576 bytes.

为什么这个配置不能正常工作?

ps.: 上传是用这个方法处理的

(服务)

  public Integer upload(String bytes, String type) throws IOException {
    String file_name = file_path + File.separator + fileName(type);

    File file = new File(file_name);
        if(!file.exists())
            if(file.getParentFile().mkdirs())
                file.createNewFile();

    FileOutputStream out = new FileOutputStream(file);
    byte[] bytes_final = bytes.split(",")[1].getBytes();
    out.write(Base64.getDecoder().decode(bytes_final));

    Arquivo arquivo = new Arquivo();
    arquivo.setFileName(file_name);
    arquivo.setType(type);
    this.dao.insert(arquivo);

    return arquivo.getId();
  }

(控制器)

  @RequestMapping(value="upload", method=RequestMethod.POST)
  @ResponseBody
  public Integer upload(@RequestParam("bytes") String arquivo, @RequestParam("type") String type) throws IOException {
    return this.serv.upload(arquivo, type);
  }

来自这个表格:

<input type="file" accept="deb" class="file-uploader" id="versaoPaga_deb" style="display: none;" th:attr="data-target=${'versaoPaga'}, data-url=@{/imagem/upload}, data-path=@{/imagem/download}" onchange="file_upload(this);"/>

并由这个 javascript 函数处理:

function file_upload(file_input) {
  var name = file_input.dataset.target;
  var url = file_input.dataset.url;
  var path = file_input.dataset.path;
  var ext;

  for(var i = 0; i<file_input.files.length; i++) {
    var file = file_input.files[i];
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var id = xhr.responseText;
            var input = document.createElement("input");
            input.setAttribute("type", "hidden");
            input.setAttribute("name", name);
            input.setAttribute("value", id);
            document.getElementById(" btn_uload_"+target+"_"+ext).append(input);
            document.getElementById("empty_file").style.display = 'none';
            document.getElementById("single_file_"+ext).style.display = 'block';
        }
    };
    var reader  = new FileReader();
    reader.onloadend = function() {
      var bytes = reader.result;
      var ext = file.name.split(".").pop();
      var formData = new FormData();
      formData.append('bytes', bytes);
      formData.append('type', ext);
      xhr.send(formData);
    }
    reader.readAsDataURL(file);
  }
}

你试过吗?如果您使用 spring-boot 2

,我认为它在 spring servlet 下
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB