春季启动文件多部分接受一半的允许大小

huangapple go评论69阅读模式
英文:

Spring boot file multipart accept half allowed size

问题

我写了一个如下的控制器:

@PostMapping(value="/upload", produces = "application/json")
@ApiOperation("上传")
@ApiIgnore
public ResponseEntity<ResponseObject> fileUpload(
        @RequestParam(value="file") MultipartFile file,
        @RequestParam (value="code",required=false, defaultValue="0") String code,
        @RequestParam (value="approvaldetails",required=false, defaultValue="0") String approvalDetails) throws Exception{
    return uploadService.uploader(file, code, approvalDetails);
}

并且我在 application.properties 中进行了以下配置:

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

但是我只能上传大约 15MB 的文件。

编辑后:
Spring Boot 版本:2.0.1

英文:

I wrote a controller as below:

@PostMapping(value=&quot;/upload&quot;, produces = &quot;application/json&quot;)
@ApiOperation(&quot;Upload&quot;)
@ApiIgnore
public ResponseEntity&lt;ResponseObject&gt; fileUpload(
		@RequestParam(value=&quot;file&quot;) MultipartFile file,
		@RequestParam (value=&quot;code&quot;,required=false, defaultValue=&quot;0&quot;)String code,
		@RequestParam (value=&quot;approvaldetails&quot;,required=false, defaultValue=&quot;0&quot;) String approvalDeatils) throws Exception{
	return uploadService.uploader(file,code,approvalDeatils);
}

and I configured below at application.porperties:

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

but I could able to upload file up to ~15MB
Editted:
Spring boot version: 2.0.1

答案1

得分: 1

1. 第一个可能的原因:

可能与您使用的 spring-boot 版本有关。

根据不同版本,MULTIPART 属性已经发生了变化。

Spring Boot 1.3.x 及更早版本:

multipart.max-file-size
multipart.max-request-size

在 Spring Boot 1.3.x 之后:

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

在 Spring Boot 2.0 之后:

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

2. 第二个可能的原因:

您的应用程序没有足够的 内存 来接收该文件。

3. 第三个可能的原因:

如果您将应用程序打包为 war 文件并部署在 tomcat 上,请确保 tomcat 允许适用于您的用例的 maxPostSize

在 conf\server.xml 中:

&lt;Connector port=&quot;80&quot; protocol=&quot;HTTP/1.1&quot;
               connectionTimeout=&quot;20000&quot;
               redirectPort=&quot;8443&quot;
                maxPostSize=&quot;67589953&quot; /&gt;
英文:

1. First potential cause:

Maybe it's related to the spring-boot version you are using.

MULTIPART properties have been changed according to versions.

Spring Boot 1.3.x and earlier

multipart.max-file-size
multipart.max-request-size

After Spring Boot 1.3.x:

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

After Spring Boot 2.0:

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

2. Second potential cause:

Your app didn't have enough memory to receive the file

3. Third potential cause:

If you are packaging as war and deploying on tomcat, make sure that tomcat allows a maxPostSize suitable for your use case.

In conf\server.xml:

&lt;Connector port=&quot;80&quot; protocol=&quot;HTTP/1.1&quot;
               connectionTimeout=&quot;20000&quot;
               redirectPort=&quot;8443&quot;
                maxPostSize=&quot;67589953&quot; /&gt;

答案2

得分: 0

@M.Mas.

  1. 我正在使用 Spring Boot 2.0.1。
  2. 即使我将内存减少到 8MB,也不依赖于内存,再次约为 4MB 将是可以接受的。
  3. 它未部署在 WAR 文件上,我正在进行调试。
    我上传了一个大小为 16.8MB 的文件,导致了以下异常:

org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: 由于其大小 (33542171) 超过了配置的最大值 (31457280),请求被拒绝。

英文:

@M.Mas.
1.I'm using spring boot 2.0.1.
2.Not depends on memory even if I decrease to 8MB, again ~4MB will be acceptable.
3. It is not deployed on the WAR file and I'm debugging.
I uploaded a file with 16.8MB and it raised below exception:

> org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (33542171) exceeds the configured maximum (31457280)

huangapple
  • 本文由 发表于 2020年10月3日 19:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64183581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定