Reputation: 280
I'm using Spring-MVC to build restful service. When serving a POST request using multipart-form, I want to limit the file size posted. Normally the approach is as follow:
<web-app version="3.0"...
//...other code omitted
<servlet>
<servlet-name>restful</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/restful-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<max-file-size>500</max-file-size>
</multipart-config>
</servlet>
And my restful-context.xml is like:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<beans:bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<beans:bean
class="org.springframework.web.multipart.support.StandardServletMultipartResolver"
id="multipartResolver" />
//...
</beans:beans>
Above is what PRO SPRING 4 tells me to do. But the max-file-size does not take effect. I tried posting a very large file(near 1GB), and no exception was thrown. Any one helps?
Later I removed the DispatcherServlet configuration from web.xml, instead I tried the annotaion style using @MultipartConfig, as follow(thanks to @RE350):
@WebServlet(loadOnStartup = 1, name = "restful", urlPatterns = { "/restful/*" }, initParams = { @WebInitParam(name = "contextConfigLocation", value = "/WEB-INF/spring/appServlet/restful-context.xml") })
@MultipartConfig(maxFileSize = 500)
public class MyDispatcherServlet extends DispatcherServlet {}
And it workded!!! So why the web.xml way does not work? Waiting for your answer.
Upvotes: 0
Views: 3083
Reputation: 1
your need both max-file-size and max-request-size and file-size-threshold
<multipart-config>
<max-file-size>500</max-file-size>
<max-request-size>700</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
Upvotes: 0
Reputation: 7196
I think max-file-size takes values as bytes,you will have to multiply 500 by 1024 * 1024 like below.
<multipart-config>
<max-file-size>524288000</max-file-size>
<max-request-size>524288000</max-request-size>
</multipart-config>
More over if you are using Spring MVC,you could have used @MultiPartConfig
annotation on your Spring service like below.
@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
Upvotes: 2