java Spring MVC - 使用 Ajax 上传多部分文件(无法解析多部分 servlet 请求)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25350296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:52:22  来源:igfitidea点击:

Spring MVC - Multipart file upload with Ajax (Could not parse multipart servlet request)

javaajaxspringspring-mvcmultipartform-data

提问by vanNiewoehner

I'm trying to upload a multipart file using Ajax, Spring MVC 3.2.0, Tomcat 8.0.9, but can't get it work. I read a lot of blogs and similar posting here on stackoverflow (Spring upload file problems, MultipartConfig with Servlet 3.0 on Spring MVC, …) which seem to have similar causes but couldn't figure out how to solve it. The weird thing is that the upload works when the file is smaller than 1MB, but when ever the recorded video exceeds that size, the following error is raised:

我正在尝试使用 Ajax、Spring MVC 3.2.0、Tomcat 8.0.9 上传多部分文件,但无法正常工作。我在 stackoverflow 上阅读了很多博客和类似的帖子(Spring 上传文件问题Spring MVC 上的 Servlet 3.0 的 MultipartConfig 等),它们似乎有类似的原因,但不知道如何解决。奇怪的是,当文件小于 1MB 时,上传工作正常,但是当录制的视频超过该大小时,会引发以下错误:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:110)

In the following you can see all the configurations I made:

在下面你可以看到我所做的所有配置:

  1. The AJAX POST-Request:

    var videoBlob = e.data;
    var pathArray = window.location.pathname.split( '/' );
    var userID;
    
    for (i = 0; i < pathArray.length; i++) {
        if (pathArray[i].toString() == "edit"){
            userID = pathArray[i+1];
        }
    }
    
    var fd = new FormData();
    fd.append('fname', 'video');
    fd.append('data', videoBlob);
    
    $.ajax({
        url: '/user/edit/uploadVideo/' + userID,
        data: fd,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function(data)
        {
            $('#result').html(data + "uploaded by FormData!");
        }
    });
    
  2. The web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:root-context.xml</param-value>
    </context-param>
    
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>common</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <filter>
        <display-name>springMultipartFilter</display-name>
        <filter-name>springMultipartFilter</filter-name>
        <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>springMultipartFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  3. The servlet-context.xml

    <mvc:annotation-driven />
    <mvc:resources mapping="/**" location="/resources/" />
    
    <context:component-scan base-package="de.talentwuerfel"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/schema"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    <property name="validationQuery" value="SELECT 1"/>
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
        <array>
            <value>de.talentwuerfel</value>
        </array>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    
    <bean id="persistenceExceptionTranslationPostProcessor"
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
  4. The root-context.xml where I defined the MultipartResolver

    <bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize"   value="100000000"/>
        <property name="maxInMemorySize" value="4096"/>
    </bean>
    
  5. The Java-Controller

    @RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
    public @ResponseBody String uploadVideo(@PathVariable long id, MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
          //.... file handling
    }
    
  1. AJAX POST 请求:

    var videoBlob = e.data;
    var pathArray = window.location.pathname.split( '/' );
    var userID;
    
    for (i = 0; i < pathArray.length; i++) {
        if (pathArray[i].toString() == "edit"){
            userID = pathArray[i+1];
        }
    }
    
    var fd = new FormData();
    fd.append('fname', 'video');
    fd.append('data', videoBlob);
    
    $.ajax({
        url: '/user/edit/uploadVideo/' + userID,
        data: fd,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function(data)
        {
            $('#result').html(data + "uploaded by FormData!");
        }
    });
    
  2. web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:root-context.xml</param-value>
    </context-param>
    
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>common</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <filter>
        <display-name>springMultipartFilter</display-name>
        <filter-name>springMultipartFilter</filter-name>
        <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>springMultipartFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  3. servlet-context.xml

    <mvc:annotation-driven />
    <mvc:resources mapping="/**" location="/resources/" />
    
    <context:component-scan base-package="de.talentwuerfel"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/schema"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    <property name="validationQuery" value="SELECT 1"/>
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
        <array>
            <value>de.talentwuerfel</value>
        </array>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    
    <bean id="persistenceExceptionTranslationPostProcessor"
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
  4. 我定义 MultipartResolver 的 root-context.xml

    <bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize"   value="100000000"/>
        <property name="maxInMemorySize" value="4096"/>
    </bean>
    
  5. Java 控制器

    @RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
    public @ResponseBody String uploadVideo(@PathVariable long id, MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
          //.... file handling
    }
    

How can I solve this problem?

我怎么解决这个问题?

EDIT:

编辑:

I tried the suggested approach and used the Servlet implementation to manage my video-file upload. The following adjustments have been made, but it's still resulting in a similar error:

我尝试了建议的方法并使用 Servlet 实现来管理我的视频文件上传。进行了以下调整,但仍然导致类似错误:

  1. Adjusted @Controller:

    @RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
    public String uploadVideo(@PathVariable long id, @RequestParam("data") Part file) {
    
    //...
    }
    
  2. The root-controller has been deleted and I added the multipartResolver to the servlet-context.xml

    <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
    </bean>
    
  3. The tag was in the web.xml has been extended by the following Multipart-Configuration:

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <multipart-config>
            <location>/tmp</location>
            <max-file-size>20848820</max-file-size>
            <max-request-size>418018841</max-request-size>
            <file-size-threshold>1048576</file-size-threshold>
        </multipart-config>
    </servlet>
    
  1. 调整后的@Controller:

    @RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
    public String uploadVideo(@PathVariable long id, @RequestParam("data") Part file) {
    
    //...
    }
    
  2. 根控制器已被删除,我将 multipartResolver 添加到 servlet-context.xml

    <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
    </bean>
    
  3. web.xml 中的标签已被以下 Multipart-Configuration 扩展:

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <multipart-config>
            <location>/tmp</location>
            <max-file-size>20848820</max-file-size>
            <max-request-size>418018841</max-request-size>
            <file-size-threshold>1048576</file-size-threshold>
        </multipart-config>
    </servlet>
    

However, I'm still getting an exception and can't upload a blob file larger than 1MB:

但是,我仍然遇到异常并且无法上传大于 1MB 的 blob 文件:

Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 

I implemented a similar file upload where a single file was simply picked and it totally worked to send large files while using the same configuration. So I believe it has rather something to do with the Ajax POST or the attached blob file?!

我实现了一个类似的文件上传,其中简单地选择了一个文件,它完全可以在使用相同配置的同时发送大文件。所以我相信它与 Ajax POST 或附加的 blob 文件有关?!

回答by justin hong

you can add this in you application to active Servlet3.0 MultiParsing:

您可以在您的应用程序中将此添加到活动的 Servlet3.0 MultiParsing:

 @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultiPartConfigFactory factory = new MultiPartConfigFactory();
        factory.setMaxFileSize("128KB");
        factory.setMaxRequestSize("128KB");
        return factory.createMultipartConfig();
    }

or do it in XML.

或在 XML 中进行。

回答by m4rtin

Not really an answer to your exact question, just my 2 cents. There are basically 2 ways of uploading files with the help of Spring MVC :

不是你确切问题的真正答案,只是我的 2 美分。在 Spring MVC 的帮助下,基本上有两种上传文件的方式:

  • using Jakarta Commons FileUpload, the only way (aside from implementing one yourself) before the appearance of the servlet 3.0 API
  • using the Servletimplementation of your server (only ifservlet impl version >= 3.0)
  • 使用Jakarta Commons FileUpload,这是 servlet 3.0 API 出现之前的唯一方法(除了自己实现)
  • 使用服务器的Servlet实现(仅当servlet impl version >= 3.0)

Since you are using Tomcat 8.0.9, the Servet 3.0 option is availableto you which I definitely recommend since it doesn't introduce yet another external dependency in your project. Also, since it follows the Servlet 3.0 spec, the configuration of such upload mechanism is now javastandardwhich is nice in case you decide to move from Spring MVC to another MVC framework (your configuration values would remain the same).

由于使用的是Tomcat 8.0.9,在SERVET 3.0选项提供给你,我肯定会推荐,因为它在你的项目没有引入另一个外部依赖。此外,由于它遵循 Servlet 3.0 规范,因此这种上传机制的配置现在是Java标准,如果您决定从 Spring MVC 移动到另一个 MVC 框架(您的配置值将保持不变),这很好。

In case you can't figure out your IOFileUploadException, I think you should give it a try.

如果你无法弄清楚你的IOFileUploadException,我认为你应该试一试。