Two controllers have different max file size in Spring Boot

蹲街弑〆低调 提交于 2020-01-25 21:38:26

问题


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 {

and

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

If using Spring Boot Controller, @MultipartConfig seems to be not worked.

@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?

EDIT:

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

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

See also: SpringBoot's @MultipartConfig maxFileSize not taking effect


回答1:


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

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);
}


来源:https://stackoverflow.com/questions/45653571/two-controllers-have-different-max-file-size-2mb-and-infinite-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!