2014-12-02 46 views
0

我正在使用Spring-MVC构建宁静的服务。 使用多部分表单提供POST请求时,我想限制发布的文件大小。 通常的方法是如下:Servlet 3.0 multipart-config无法在最大文件大小下工作

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

我的宁静-context.xml的是这样的:

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

上面是什么PRO SPRING 4告诉我这样做。 但最大文件大小不生效。我试着发布一个非常大的文件(接近1GB),并没有抛出异常。 任何人都可以帮忙吗?


后来我删除了DispatcherServlet的配置从web.xml中,而不是我试过annotaion风格采用@MultipartConfig,如下(感谢@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 {} 

它workded! 那么为什么web.xml的方式不起作用?等待你的答复。

+0

你在哪里托管应用程序,可它不是一个Servlet容器3+ ? – 2014-12-02 16:33:14

+0

@BijuKunjummen我正在使用STS内置的Pivotal tc Server,它应该使用嵌入式tomcat 8.0.9 – ldn0x7dc 2014-12-03 02:24:57

回答

2

我认为max-file-size的值是以字节为单位的,你必须像下面那样乘以500 1024 * 1024。

 <multipart-config> 
     <max-file-size>524288000</max-file-size> 
     <max-request-size>524288000</max-request-size> 
    </multipart-config> 

更多了,如果你正在使用Spring MVC的,你也可以使用@MultiPartConfig标注上你的Spring服务就像下面。

@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024, 
    maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5) 
0

你既需要最大文件大小和最大请求​​大小和文件大小阈值

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