本文使用 Spring Web model-view-controller (MVC) framework
建立一個簡單的一個檔案上傳程式
1、本文中使用的相關技術
Eclipse 4.2 Version: Juno Service Release 2
Maven 3.0.5
JAVA JDK 1.6.0_24
Spring 3.2.2.RELEASE。
P.S Spring 3.x, 需 JDK 1.5 以上。
2、建立一個Maven專案
P.S 請參考前一篇文章 建立一個基本簡單的Spring MVC project
3、加入 Spring 3.0 jar
加入在Maven專案中的pom.xml
程式碼:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>SpringMVC_File_Upload_sample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC_File_Upload_sample Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>SpringMVC_File_Upload_sample</finalName>
</build>
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jstl dependencie -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- file dependencie -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>
4. 建立JSP 頁面
目錄 : src/main/webapp/
檔名 : index.jsp
<jsp:forward page="show.html"></jsp:forward>
目錄 : src/main/webapp/WEB-INF/jsp
檔名 : file_upload_form.jsp
程式碼:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Multiple File Upload</title>
</head>
<body>
<h1>Spring Multiple File Upload example</h1>
<form:form method="post" action="save.html"
modelAttribute="uploadForm" enctype="multipart/form-data">
<p>please select file </p>
<table>
<tr>
<td><input name="files[0]" type="file" /></td>
</tr>
</table>
<br/><input type="submit" value="Upload" />
</form:form>
</body>
</html>
目錄 : src/main/webapp/WEB-INF/jsp
檔名 : file_upload_success.jsp
程式碼:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Spring MVC Multiple File Upload</title>
</head>
<body>
<h1>Spring Multiple File Upload example</h1>
<p>Following files are uploaded successfully.</p>
<ol>
<c:forEach items="${files}" var="file">
<li>${file}</li>
</c:forEach>
</ol>
</body>
</html>
檔名 : web.xml
程式碼:
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
7. 建立可
以暫時存放檔案的資料型態
如:
目錄 : src/main/java/com/springmvc/fileupload/sample/form
檔名 : FileUploadForm.java
程式碼:
8. 建立Controller & Mapping
在Spring 3 裡大量使用 annotation 。
如:
@Controller
目錄 : src/main/java/com/springmvc/fileupload/sample/controller
檔名 : FileUploadController.java
程式碼:
加入在Maven專案中的pom.xml
程式碼:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>SpringMVC_File_Upload_sample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC_File_Upload_sample Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>SpringMVC_File_Upload_sample</finalName>
</build>
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jstl dependencie -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- file dependencie -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>
目錄 : src/main/webapp/
檔名 : index.jsp
<jsp:forward page="show.html"></jsp:forward>
檔名 : file_upload_form.jsp
程式碼:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Multiple File Upload</title>
</head>
<body>
<h1>Spring Multiple File Upload example</h1>
<form:form method="post" action="save.html"
modelAttribute="uploadForm" enctype="multipart/form-data">
<p>please select file </p>
<table>
<tr>
<td><input name="files[0]" type="file" /></td>
</tr>
</table>
<br/><input type="submit" value="Upload" />
</form:form>
</body>
</html>
目錄 : src/main/webapp/WEB-INF/jsp
檔名 : file_upload_success.jsp
程式碼:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Spring MVC Multiple File Upload</title>
</head>
<body>
<h1>Spring Multiple File Upload example</h1>
<p>Following files are uploaded successfully.</p>
<ol>
<c:forEach items="${files}" var="file">
<li>${file}</li>
</c:forEach>
</ol>
</body>
</html>
5. 加入spring設定到Web application web.xml
程式碼:
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
6. 建立 Spring mvc Config xml 檔
檔名: spring-servlet.xml
程式碼:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.springmvc.fileupload.sample.controller"/>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
以暫時存放檔案的資料型態
如:
目錄 : src/main/java/com/springmvc/fileupload/sample/form
檔名 : FileUploadForm.java
程式碼:
package com.springmvc.fileupload.sample.form; import java.util.List; import org.springframework.web.multipart.MultipartFile; public class FileUploadForm { private Listfiles; public List getFiles() { return files; } public void setFiles(List files) { this.files = files; } }
在Spring 3 裡大量使用 annotation 。
如:
@Controller
目錄 : src/main/java/com/springmvc/fileupload/sample/controller
檔名 : FileUploadController.java
程式碼:
package com.springmvc.fileupload.sample.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import com.springmvc.fileupload.sample.form.FileUploadForm; @Controller public class FileUploadController { @RequestMapping(value = "/show", method = RequestMethod.GET) public String displayForm() { return "file_upload_form"; } @RequestMapping(value = "/save", method = RequestMethod.POST) public String save( @ModelAttribute("uploadForm") FileUploadForm uploadForm, Model map) { Listfiles = uploadForm.getFiles(); List fileNames = new ArrayList (); if( files != null && files.size() > 0 ) { for (MultipartFile multipartFile : files) { String fileName = multipartFile.getOriginalFilename(); fileNames.add(fileName); } } map.addAttribute("files", fileNames); return "file_upload_success"; } }
9. 結果
其它文章
沒有留言:
張貼留言