【问题标题】:Servlet 3.0 multipart-config doesn't work on max-file-sizeServlet 3.0 multipart-config 不适用于 max-file-size
【发布时间】:2015-01-30 23:34:44
【问题描述】:

我正在使用 Spring-MVC 来构建 restful 服务。 使用 multipart-form 服务 POST 请求时,我想限制发布的文件大小。 通常方法如下:

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

而我的 restful-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 告诉我要做的事情。 但是 max-file-size 不生效。我尝试发布一个非常大的文件(接近 1GB),没有抛出异常。 有人帮忙吗?


后来我从 web.xml 中删除了 DispatcherServlet 配置,改为使用@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 {}

而且成功了!!! 那么为什么 web.xml 方式不起作用呢?等待您的答复。

【问题讨论】:

  • 您在哪里托管应用程序,可能不是 Servlet 3+ 容器?
  • @BijuKunjummen 我正在使用 STS 内置的 Pivotal tc Server,它应该使用嵌入式 tomcat 8.0.9

标签: spring-mvc servlets servlet-3.0 multipartconfig


【解决方案1】:

你需要 max-file-size 和 max-request-size 和 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>

【讨论】:

    【解决方案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,你可以在你的 Spring 服务上使用 @MultiPartConfig 注释,如下所示。

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

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2021-07-10
      • 2011-02-02
      • 2019-03-13
      • 2019-10-06
      相关资源
      最近更新 更多