使用 springMVC 的 CommonsMultipartFile 類讀取文件,實現文件上傳。
須要使用文件上傳的2個jar包:Commons-logging.jar、commons-io-xxx.jarto。
經過 Maven 管理項目能夠直接使用 pom.xml 配置,在線下載須要的 jar 包,包括 spring 框架和 SSM 整合。
pom.xmljavascript
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.project</groupId> 5 <artifactId>FileUpload</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>FileUpload Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 18 <!-- fileUpload --> 19 <dependency> 20 <groupId>commons-io</groupId> 21 <artifactId>commons-io</artifactId> 22 <version>2.5</version> 23 </dependency> 24 25 <dependency> 26 <groupId>commons-fileupload</groupId> 27 <artifactId>commons-fileupload</artifactId> 28 <version>1.3.2</version> 29 </dependency> 30 31 <!--spring--> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-core</artifactId> 35 <version>4.2.3.RELEASE</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-context</artifactId> 41 <version>4.2.3.RELEASE</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-context-support</artifactId> 47 <version>4.2.3.RELEASE</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-beans</artifactId> 53 <version>4.2.3.RELEASE</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-aop</artifactId> 59 <version>4.2.3.RELEASE</version> 60 </dependency> 61 62 <dependency> 63 <groupId>org.aspectj</groupId> 64 <artifactId>aspectjweaver</artifactId> 65 <version>1.6.8</version> 66 </dependency> 67 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-tx</artifactId> 71 <version>4.2.3.RELEASE</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-orm</artifactId> 77 <version>4.2.3.RELEASE</version> 78 </dependency> 79 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-jdbc</artifactId> 83 <version>4.2.3.RELEASE</version> 84 </dependency> 85 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-web</artifactId> 89 <version>4.2.3.RELEASE</version> 90 </dependency> 91 <dependency> 92 <groupId>org.springframework</groupId> 93 <artifactId>spring-webmvc</artifactId> 94 <version>4.2.3.RELEASE</version> 95 </dependency> 96 97 <!-- mybatis --> 98 <dependency> 99 <groupId>org.mybatis</groupId> 100 <artifactId>mybatis</artifactId> 101 <version>3.2.2</version> 102 </dependency> 103 104 <dependency> 105 <groupId>org.mybatis</groupId> 106 <artifactId>mybatis-spring</artifactId> 107 <version>1.1.1</version> 108 </dependency> 109 110 111 <!--log4j--> 112 <dependency> 113 <groupId>log4j</groupId> 114 <artifactId>log4j</artifactId> 115 <version>1.2.17</version> 116 </dependency> 117 118 <!--mysql driver--> 119 <dependency> 120 <groupId>mysql</groupId> 121 <artifactId>mysql-connector-java</artifactId> 122 <version>5.1.17</version> 123 </dependency> 124 125 <!--dbcp--> 126 <dependency> 127 <groupId>org.apache.commons</groupId> 128 <artifactId>commons-dbcp2</artifactId> 129 <version>2.1.1</version> 130 </dependency> 131 132 133 </dependencies> 134 <build> 135 <finalName>FileUpload</finalName> 136 </build> 137 </project>
app-action.xmlhtml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!--提供註解和驗證支持 --> <mvc:annotation-driven conversion-service="typeChange"></mvc:annotation-driven> <!--類型轉換器 --> <bean id="typeChange" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.project.util.DateChange"></bean> </set> </property> </bean> <!--文件上傳的支持類 ,id必須爲multipartResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> <!--掃描指定包中的spring組件 --> <context:component-scan base-package="com.project.action"></context:component-scan> </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns="http://www.springframework.org/schema/beans" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 10 xmlns:jaxws="http://cxf.apache.org/jaxws" 11 xsi:schemaLocation="http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 13 http://www.springframework.org/schema/data/jpa 14 http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 17 http://www.springframework.org/schema/context 18 http://www.springframework.org/schema/context/spring-context-4.0.xsd 19 http://www.springframework.org/schema/aop 20 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 21 http://www.springframework.org/schema/mvc 22 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 23 http://cxf.apache.org/jaxws 24 http://cxf.apache.org/schemas/jaxws.xsd "> 25 26 <bean id="tc" class="org.springframework.context.support.ConversionServiceFactoryBean"> 27 <property name="converters"> 28 <set> 29 <bean class="com.project.util.DataChange"></bean> 30 </set> 31 </property> 32 </bean> 33 34 <mvc:annotation-driven conversion-service="tc"></mvc:annotation-driven> 35 36 <context:component-scan base-package="com.project.action"></context:component-scan> 37 38 <!-- 文件上傳解析器 --> 39 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 40 <property name="defaultEncoding" value="utf-8"></property> 41 <property name="maxUploadSize" value="10485760000"></property><!-- 最大上傳文件大小 --> 42 <property name="maxInMemorySize" value="10960"></property> 43 </bean> 44 </beans>
applicationContext.xml
該配置文件主要設置業務層的一些基本配置:
● 數據源
● session工廠
● 會話
● 事務管理器
● 註解事務支持java
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns="http://www.springframework.org/schema/beans" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 10 xmlns:jaxws="http://cxf.apache.org/jaxws" 11 xsi:schemaLocation="http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 13 http://www.springframework.org/schema/data/jpa 14 http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 17 http://www.springframework.org/schema/context 18 http://www.springframework.org/schema/context/spring-context-4.0.xsd 19 http://www.springframework.org/schema/aop 20 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 21 http://www.springframework.org/schema/mvc 22 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 23 http://cxf.apache.org/jaxws 24 http://cxf.apache.org/schemas/jaxws.xsd "> 25 26 <bean id="data" class="org.apache.commons.dbcp2.BasicDataSource"> 27 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 28 <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&allowMultiQueries=true"></property> 29 <property name="username" value="root"></property> 30 <property name="password" value="123456"></property> 31 32 <property name="maxTotal" value="100"></property> 33 <property name="maxIdle" value="20"></property> 34 <property name="maxWaitMillis" value="300"></property> 35 </bean> 36 37 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 38 <property name="dataSource" ref="data"></property> 39 <property name="typeAliasesPackage" value="com.project.bean"></property> 40 <property name="mapperLocations"> 41 <list> 42 <value>classpath:com/project/mapper/*.xml</value> 43 </list> 44 </property> 45 </bean> 46 47 <bean id="session" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> 48 <constructor-arg ref="sqlSessionFactory"></constructor-arg> 49 </bean> 50 51 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 52 <property name="dataSource" ref="data"></property> 53 </bean> 54 55 <tx:annotation-driven transaction-manager="transactionManager"/> 56 57 <context:component-scan base-package="com.project.service"></context:component-scan> 58 59 </beans>
1.工具類 util
時間類型裝換類:DateChange.javamysql
過濾器:web
文件上傳處理工具類:UploadUtil.javaspring
1.持久層sql
2. 業務層:
IUserServiceapache
BaseServicespring-mvc
UserServiceImplsession
3.表現層:
文件上傳頁面:
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script> function setImagePreview() { var docObj=document.getElementById("doc"); var imgObjPreview=document.getElementById("preview"); if(docObj.files && docObj.files[0]){ //火狐下,直接設img屬性 imgObjPreview.style.display = 'block'; imgObjPreview.style.width = '300px'; imgObjPreview.style.height = '120px'; //imgObjPreview.src = docObj.files[0].getAsDataURL(); //火狐7以上版本不能用上面的getAsDataURL()方式獲取,須要一下方式 imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); }else{ //IE下,使用濾鏡 docObj.select(); var imgSrc = document.selection.createRange().text; var localImagId = document.getElementById("localImag"); //必須設置初始大小 localImagId.style.width = "250px"; localImagId.style.height = "200px"; //圖片異常的捕捉,防止用戶修改後綴來僞造圖片 try{ localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; }catch(e){ alert("您上傳的圖片格式不正確,請從新選擇!"); return false; } imgObjPreview.style.display = 'none'; document.selection.empty(); } return true; } </script> </head> <body> <form action="/FileUpload/user/upload.do" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="userName"><br> 生日:<input type="text" name="birthday"><br> <p><p id="localImag"><img id="preview" width=-1 height=-1 style="diplay:none" /></p></p> 圖片:<input type="file" name="img" id="doc" onchange="javascript:setImagePreview();"><br> <input type="submit" value="提交"> </form> </body> </html>
jsp 頁面:表單屬性
表單中enctype="multipart/form-data"的意思,是設置表單的MIME編碼。
默認狀況,這個編碼格式是application/x-www-form-urlencoded,不能用於文件上傳;只有使用了multipart/form-data,才能完整的傳遞文件數據,進行下面的操做.