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

我正在尝试使用Ajax,Spring MVC 3.2.0,Tomcat 8.0.9上传多部分文件,但无法使其工作.我在stackoverflow(Spring upload file problems,MultipartConfig with Servlet 3.0 on Spring MVC,…)上阅读了很多博客和类似的帖子,这似乎有类似的原因,但无法弄清楚如何解决它.奇怪的是,当文件小于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)

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

> 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!");
    }
});

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

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

>我定义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>

> Java控制器

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

我怎么解决这个问题?

编辑:

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

>调整后的@Controller:

@RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
public String uploadVideo(@PathVariable long id, @RequestParam("data") Part file) {

//...
}

> root控制器已被删除,我将multipartResolver添加到servlet-context.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

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

但是,我仍然收到异常,无法上传大于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) 

我实现了一个类似的文件上传,其中只选择了一个文件,并且在使用相同的配置时它完全可以发送大文件.所以我认为它与Ajax POST或附加的blob文件有什么关系?!

最佳答案
您可以在应用程序中将其添加到活动的Servlet3.0 MultiParsing中:

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

或者用XML做.

转载注明原文:java – Spring MVC – 使用Ajax上传多部分文件(无法解析多部分servlet请求) - 代码日志