gpt4 book ai didi

java - SpringBoot 的 @MultipartConfig maxFileSize 没有生效

转载 作者:IT老高 更新时间:2023-10-28 13:46:35 28 4
gpt4 key购买 nike

我有一个带有 MultipartConfig 注释的 Controller (其中的一个片段如下所示):

@RestController
@RequestMapping("packages")
@MultipartConfig(maxFileSize = 1024*1024*1024, maxRequestSize = 1024*1024*1024)
public class PackagesController
{
@RequestMapping(method = RequestMethod.POST)
public String create(@RequestParam("package") MultipartFile uploadedPackage)
{
// do stuff to the file
return "create";
}
}

不过,当我将文件上传到此端点时,它似乎正在使用默认的多部分配置值:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field package exceeds its maximum permitted size of 1048576 bytes.
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:633) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_45]
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.catalina.connector.Request.parseParts(Request.java:2776) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
... 30 common frames omitted

我的应用程序如下所示:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}

}

我是否缺少使 @MultipartConfig 注释生效的东西?

最佳答案

使用 Spring Boot 2.0,您应该在 application.yml 中使用它

spring:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB

来自 documentation :

Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default, Spring Boot configures Spring MVC with a maximum size of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, the location to which intermediate data is stored (for example, to the /tmp directory), and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. For example, if you want to specify that files be unlimited, set the spring.servlet.multipart.max-file-size property to -1.

摘自 Appendix A of documentation

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.

关于java - SpringBoot 的 @MultipartConfig maxFileSize 没有生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945482/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com