gemr1423
gemr1423

Reputation: 749

How to limit size of multipart file in a java controller

I have a rest controller method that receive a multipart file parameter, I need to constraint the file size limit of it. I tried different optiones but it's not working yet for me. I´m using Apache Tomcat 8. I'm calling the rest method by a windows service.

Following the definition of the class and the method:

@Controller
public class MyClass{
     ...
}

@RequestMapping(value = "/path/method/param1/{param1}/param2/{param2}/", 
                method = RequestMethod.POST)
public ResponseEntity<String> method(DTO dto, @RequestParam("file") MultipartFile multipartFile){
    ...
}

Solutions tested:

  1. Adding the following node in the web.xml file
<multipart-config>
   <max-file-size>52428800</max-file-size>
   <max-request-size>52428800</max-request-size>
   <file-size-threshold>0<</file-size-threshold>
</multipart-config>
  1. Adding maxPostSize and maxSwallowSize properties to the connector node in the server.xml file

  2. Adding parameters of size to the .properties file

  3. Adding the @MultipartConfig annotation in the controller class

Any idea or suggestions?

Upvotes: 4

Views: 1110

Answers (1)

David Monsalve
David Monsalve

Reputation: 46

Check the spring-servlet.xml file, maybe there is a created bean of the class org.springframework.web.multipart.commons.CommonsMultipartResolver, it could be above of the rest of configurations or methods to handle this

Upvotes: 3

Related Questions