本文使用 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。
加入在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>
程式碼:
<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>
以暫時存放檔案的資料型態
如:
目錄 : 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 List files;
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) {
List files = 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";
}
}
- 首頁
- JAVA 目錄
- JAVA WEB 目錄
- JAVA 其他未分類 目錄
- Grails目錄
- jQuery 目錄
- Apache 目錄
- JavaScript 目錄
- Spring + Hibernate 目錄
- Hibernate 目錄
- Linux 目錄
- 程式開發工具目錄
- MySQL 目錄
- PHP目錄
- C/C++ 目錄
- Google App Engine 目錄
- HTML5/CSS3
- 程式開發基本資訊
- Android
- Oracle 目錄
- Struts 目錄
留言列表