两个控制器在Spring Boot中具有不同的最大文件大小 [英] Two controllers have different max file size in Spring Boot

查看:227
本文介绍了两个控制器在Spring Boot中具有不同的最大文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Servlet 3.0规范中,可以创建两个具有不同最大文件大小的servlet并且工作正常。

In Servlet 3.0 specification, two servlets have different max file size can be created and worked fine.

@WebServlet(urlPatterns = { "/ureupload1" })
// 10MB
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
public class UploadServlet1 extends HttpServlet {

@WebServlet(urlPatterns = { "/ureupload2" })
// 30MB
@MultipartConfig(maxFileSize = 1024 * 1024 * 30)
public class UploadServlet2 extends HttpServlet {






如果使用Spring Boot Controller, @MultipartConfig 似乎不工作。

@Controller
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
public class UploadController1 {

    @RequestMapping(value = "/upload1", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ModelAndView doPost(@RequestParam("file") MultipartFile file,

如何创建两个具有不同最大文件大小的控制器?

How do I create two controllers have different max file size?

编辑:

其他信息:以下属性位于 application.properties 中,以便设置默认的最大文件大小:

Additional information: The following properties are in application.properties in order to set default max file size:

spring.http.multipart.max-file-size=30MB

另请参阅: SpringBoot的@MultipartConfig maxFileSize未生效

推荐答案

您需要在application.properties文件中设置默认配置

You need to set a default configuration in application.properties file

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

在你的控制器中你需要抛出 MaxUploadSizeExceededException 基于文件大小的异常:

And in your controller you need to throw MaxUploadSizeExceededException exception based on the file size :

long  limit = 1024 * 1024 * 10; 
if (file.getSize() > limit) {
    throw new MaxUploadSizeExceededException(limit);
}

这篇关于两个控制器在Spring Boot中具有不同的最大文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆