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

问题描述 投票:0回答:1

我将此行添加到我的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 spring-boot tomcat multipartform-data multipart
1个回答
0
投票

您尝试过这个吗?如果您使用spring-boot2,我认为它在spring servelet下是打开的]

spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
© www.soinside.com 2019 - 2024. All rights reserved.