gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:10 25 4
gpt4 key购买 nike

我正在尝试使用 Ajax、Spring MVC 3.2.0、Tomcat 8.0.9 上传一个多部分文件,但无法正常工作。我在 stackoverflow 上阅读了很多博客和类似的帖子(Spring upload file problemsMultipartConfig 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)

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

  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 Controller

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

我该如何解决这个问题?

编辑:

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

  1. 调整@Controller:

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

    //...
    }
  2. root-controller 已删除,我将 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>

但是,我仍然遇到异常,无法上传大于 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 文件有相当大的关系?!

最佳答案

您可以在您的应用程序中将此添加到 Activity 的 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 请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25350296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com