问题
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