更新时间:2022-12-30 07:41:05
乍一看似乎不是***的解决方案,但这取决于您的要求.
It may seem like not the best solution at first sight, but it depends on your requirements.
您可以为所有控制器设置一个具有最大上传大小的全局选项,并为特定控制器使用较小的值覆盖它.
You can set a global option with maximum upload size for all controllers and override it with lower value for specific controllers.
application.properties 文件(Spring Boot)
application.properties file (Spring Boot)
spring.http.multipart.max-file-size=50MB
使用检查上传控制器
public void uploadFile(@RequestPart("file") MultipartFile file) {
final long limit = 2 * 1024 * 1024; // 2 MB
if (file.getSize() > limit) {
throw new MaxUploadSizeExceededException(limit);
}
StorageService.uploadFile(file);
}
如果文件大于 2MB,您将在日志中看到此异常:
In case of a file bigger than 2MB you'll get this exception in log:
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2097152 bytes exceeded