2013年5月5日 星期日

[JAVA_Spring]建立一個Spring MVC fileupload project


本文使用 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>


5. 加入spring設定到Web application web.xml
檔名 : 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>




7. 建立可
以暫時存放檔案的資料型態

如:
目錄 : 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;
 }
}


8. 建立Controller & Mapping
    在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";
 }
}


9. 結果



















其它文章

沒有留言:

張貼留言

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...