springMVC两种方式实现多文件上传及效率比较

Easter79
• 阅读 618

springMVC实现多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传。这两种方式对于实现多文件上传效率上却有着很大的差距,下面我们通过实例来看一下这两种方式的实现方式,同时比较一下在效率上到底存在着多大的差距。

1.下载相关jar包。需要引入的jar出了springMVC的jar包外,还需要引入com.springsource.org.apache.commons.fileupload-1.2.0.jar和com.springsource.org.apache.commons.io-1.4.0.jar。所有的jar包可以通过“点击这里”进行下载。

2.配置springAnnotation-servlet.xml文件(文件名称可以自定义,只要和web.xml中引入的名称一样即可):

[html] view plaincopyspringMVC两种方式实现多文件上传及效率比较 springMVC两种方式实现多文件上传及效率比较

  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns:p="http://www.springframework.org/schema/p"

  4. xmlns:mvc="http://www.springframework.org/schema/mvc"

  5. xmlns:context="http://www.springframework.org/schema/context"

  6. xmlns:util="http://www.springframework.org/schema/util"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

  10. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"\>

  11. <context:component-scan base-package="com.tgb.web.controller.annotation"></context:component-scan>

  12. mvc:annotation-driven/

  13. <mvc:resources location="/img/" mapping="/img/**"/>

  14. <mvc:resources location="/js/" mapping="/js/**"/>

  15. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  16. <property name="prefix" value="/"></property>

  17. <property name="suffix" value=".jsp"></property>

  18. </bean>

  19. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

  20. <property name="defaultEncoding" value="utf-8"></property>

  21. <property name="maxUploadSize" value="10485760000"></property>

  22. <property name="maxInMemorySize" value="40960"></property>

  23. </bean>

  24. </beans>

  25. 配置web.xml文件:

[html] view plaincopyspringMVC两种方式实现多文件上传及效率比较 springMVC两种方式实现多文件上传及效率比较

  1. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd" id="WebApp_ID" version="2.5">

  2. <display-name>springMVC1</display-name>

  3. <welcome-file-list>

  4. <welcome-file>index.html</welcome-file>

  5. </welcome-file-list>

  6. <servlet>

  7. <servlet-name>springMVC</servlet-name>

  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  9. <init-param>

  10. <param-name>contextConfigLocation</param-name>

  11. <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>

  12. </init-param>

  13. <load-on-startup>1</load-on-startup>

  14. </servlet>

  15. <filter>

  16. <filter-name>encodingFilter</filter-name>

  17. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  18. <init-param>

  19. <param-name>encoding</param-name>

  20. <param-value>UTF-8</param-value>

  21. </init-param>

  22. <init-param>

  23. <param-name>forceEncoding</param-name>

  24. <param-value>true</param-value>

  25. </init-param>

  26. </filter>

  27. <filter-mapping>

  28. <filter-name>encodingFilter</filter-name>

  29. <url-pattern>/*</url-pattern>

  30. </filter-mapping>

  31. <servlet-mapping>

  32. <servlet-name>springMVC</servlet-name>

  33. <url-pattern>/</url-pattern>

  34. </servlet-mapping>

  35. </web-app>

  36. jsp页面代码:

[html] view plaincopyspringMVC两种方式实现多文件上传及效率比较 springMVC两种方式实现多文件上传及效率比较

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <html>

  4. <head>