M S*_*nil 7 spring spring-mvc spring-boot embedded-tomcat
我尝试使用我的 Spring Boot API 上传文件。当我使用小文件(小于 1 MB)时,该函数工作正常,但当我上传大文件时,它给了我一个例外。我正在使用嵌入式 Tomcat 服务器。
Maximum upload size exceeded;
nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
Run Code Online (Sandbox Code Playgroud)
我在我的文件中尝试了以下代码,但每次我都会收到错误
1. 应用程序.属性
server.tomcat.max-swallow-size=100MB
server.tomcat.max-http-post-size=100MB
spring.servlet.multipart.enabled=true
spring.servlet.multipart.fileSizeThreshold=100MB
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
Run Code Online (Sandbox Code Playgroud)
我也尝试过
spring.servlet.multipart.maxFileSize=100MB
spring.servlet.multipart.maxRequestSize=100MB
Run Code Online (Sandbox Code Playgroud)
2.以下是我的文件上传代码
public RestDTO uploadFile(MultipartFile file, String subPath) {
if (file.isEmpty()) {
return new RestFailure("Failed to store empty file");
}
try {
String fileName = new Date().getTime() + "_" + file.getOriginalFilename();
String filePath = uploadPath + subPath + fileName;
if (Objects.equals(file.getOriginalFilename(), "blob")) {
filePath += ".png";
fileName += ".png";
}
File uploadDir = new File(uploadPath + subPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
FileOutputStream output = new FileOutputStream(filePath);
output.write(file.getBytes());
LOGGER.info("File path : " + filePath);
MediaInfoDTO mediaInfoDTO = getThumbnailFromVideo(subPath, fileName);
String convertedFileName = convertVideoToMP4(subPath, fileName);
System.out.println("---------------->" + convertedFileName);
return new RestData<>(new MediaDetailDTO(mediaInfoDTO.getMediaPath(), convertedFileName,
mediaInfoDTO.getMediaType(), mediaInfoDTO.getMediaCodec(), mediaInfoDTO.getWidth(),
mediaInfoDTO.getHeight(), mediaInfoDTO.getDuration()));
} catch (IOException e) {
LOGGER.info("Can't upload file: " + e.getMessage());
return new RestFailure("Failed to store empty file");
}
}
Run Code Online (Sandbox Code Playgroud)
但每次我都会遇到同样的例外。
除了评论之外,我可能建议@Bean
为 Factory创建一个MultipartConfigurationElement
,如果您有 TomCat 方面的任何限制,这基本上应该覆盖其他限制。
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize(DataSize.ofBytes(100000000L));
factory.setMaxRequestSize(DataSize.ofBytes(100000000L));
return factory.createMultipartConfig();
}
Run Code Online (Sandbox Code Playgroud)
这里DataSize
是类型org.springframework.util.unit.DataSize
参考https://github.com/spring-projects/spring-boot/issues/11284
我怀疑另一个问题可能来自 TomCat,maxSwallowSize
如果上面不起作用,请参见 Baeldung 的第 5 点。
https://www.baeldung.com/spring-maxuploadsizeexceeded
归档时间: |
|
查看次数: |
21643 次 |
最近记录: |