文件的上傳與下載基本上是web項目中會用到的技術,在web學習中我們用到的是 Apache fileupload這個組件來實現上傳,在springmvc中對它進行了封裝,讓我們使用起來比較方便,但是底層還是由Apache fileupload來實現的。springmvc中由MultipartFile接口來實現文件上傳。
1.創建web工程,搭建SpringMVC運行環境。另外再導入兩個jar包 文件上傳jar
2. 創建前端jsp頁面
-
input的type設置為file
-
form表單的method設為post,
-
form表單的enctype設置為multipart/form-data,以二進制的形式傳輸數據。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/fileupload/upload" enctype="multipart/form-data" method="post"> 11 <input type="file" name="file"><br> 12 <input type="submit" value="上傳"> 13 </form> 14 </body> 15 </html>
3.創建FileuploadController
使用MultipartFile對象作為參數,接收前端發送過來的文件,將文件寫入本地文件中,就完成了上傳操作
1 package com.springmvc.fileupload; 2 3 import java.io.File; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod; 10 import org.springframework.web.bind.annotation.RequestParam; 11 import org.springframework.web.multipart.MultipartFile; 12 13 /** 14 * Springmvc 文件上傳 15 * 16 * @author Administrator 17 * 18 */ 19 20 @Controller 21 @RequestMapping(value = "/fileupload") 22 public class FileUploadController { 23 // 上傳頁面 24 @RequestMapping(value = "/upload", method = RequestMethod.POST) 25 public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { 26 // 判斷文件是否為空 27 if (file.isEmpty()) { 28 return "failed"; 29 } 30 // 獲取文件存儲路徑(絕對路徑) 31 String path = request.getServletContext().getRealPath("/WEB-INF/file"); 32 System.out.println(path); 33 // 獲取文件名稱 34 String fileName = file.getOriginalFilename(); 35 // 創建文件實例 36 File f = new File(path, fileName); 37 // 判斷文件目錄是否存在 38 if (!f.getParentFile().exists()) { 39 // 創建目錄 40 f.getParentFile().mkdirs(); 41 System.out.println("文件已創建"); 42 } 43 // 寫入文件 44 file.transferTo(f); 45 return "filesuccess"; 46 } 47 48 @RequestMapping(value = "/uploadPage") 49 public String upload() { 50 51 return "fileupload"; 52 } 53 54 }
4. springmvc.xml配置CommonsMultipartResolver
1 <bean id="multipartResolver" 2 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <!--上傳文件的最大大小,單位為字節 --> 4 <property name="maxUploadSize" value="17367648787"></property> 5 <!-- 上傳文件的編碼 --> 6 <property name="defaultEncoding" value="UTF-8"></property> 7 </bean>
5.測試結果(上傳成功)
注意:
(一)對於數據的請求方式:get和post,首先比較兩者最常見的不同:
①get一般用於向服務器請求獲取數據,請求參數存放在URL中,並在地址欄可見,而post是向服務器提交數據,數據放置在容器(HTML HEADER)內且不可見;
②get方式提交的數據最多只能有1024字節,而post則沒有此限制;
(二)spring的RequestMethod.GET和RequestMethod.POST,對於spring接口的method的兩種定義,在訪問時這兩種方式的效果有不同:
①將一個method定義成RequestMethod.GET時,可以直接通過地址訪問,這非常方便我們在開發的時候調用到我們的接口並進行測試;
②同樣的接口,將其method更改為RequestMethod.POST時,你會發現接口在地址欄訪問不了了,只有向服務器發起一個POST請求時(例:ajax實例:如何使用json+ajax的方法實現類似前端特效tab切換效果)才起作用