Spring MVC - MultipartFile實現檔案上傳(單檔案與多檔案上傳)
阿新 • • 發佈:2018-10-31
前提:引入jar包。
-
<dependency>
-
<groupId>commons-fileupload
</groupId>
-
<artifactId>commons-fileupload
</artifactId
>
-
<version>1.3
</version>
-
</dependency>
一、配置檔案:
SpringMVC 用的是 的MultipartFile來進行檔案上傳 所以我們首先要配置MultipartResolver:用於處理表單中的file
-
<!-- SpringMVC上傳檔案時,需要配置MultipartResolver處理器 -->
-
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
-
<property
name="defaultEncoding" value="UTF-8" />
-
<!-- 指定所上傳檔案的總大小不能超過200KB。注意maxUploadSize屬性的限制不是針對單個檔案,而是所有檔案的容量之和 -->
-
<!-- <property name="maxUploadSize" value="200000"/> -->
-
<!-- 指定上傳檔案的臨時路徑 -->
-
<!-- <property name="uploadTempDir" value="uploadTempDirectory" /> -->
-
</bean>
屬性詳解:
defaultEncoding="UTF-8" 是請求的編碼格式,預設為iso-8859-1
maxUploadSize="200000" 是上傳檔案的大小,單位為位元組
uploadTempDir="uploadTempDirectory " 為上傳檔案的臨時路徑
二、建立一個簡單的上傳表單:
-
<form action="uploadId" method="post" enctype="multipart/form-data">
-
<input type="file" name="idPic" />
-
<button >Submit
</button>
-
</form>
注意:要在form標籤中加上
enctype="multipart/form-data"
表示該表單是要處理檔案的,這是最基本的東西,很多人會忘記然而當上傳出錯後則去找程式的錯誤,卻忘了這一點
三、編寫上傳控制類
1、建立一個控制類: FileUploadController,一個返回提交檔案的頁面
2、編寫提交表單的action:
3、使用SpringMVC註解RequestParam來指定表單中的idPic引數;
4、指定一個用於儲存檔案的web專案路徑
5、通過MultipartFile的transferTo(File dest)這個方法來轉存檔案到指定的路徑。MultipartFile轉存後,無法再操作,會報錯
-
<span style=
"white-space:pre"> </span>
//通過Spring的autowired註解獲取spring預設配置的request
-
@Autowired
-
private HttpServletRequest request;
-
-
<span style=
"white-space:pre"> </span>
/**
-
* 跳轉到上傳檔案的頁面
-
* @return
-
*/
-
@RequestMapping(value =
"/uploadPage")
-
public String uploadPage() {
-
return
"upload";
-
}
-
-
/**
-
* 上傳檔案 用@RequestParam註解來指定表單上的file為MultipartFile
-
* @param multipartfile
-
* @return
-
* @throws IOException
-
*/
-
@RequestMapping(value =
"/uploadId")
-
@ResponseBody
-
public ResponseEntity<
byte[]> idIdentification(
@RequestParam(
"idPic") MultipartFile multipartfile)
throws IOException {
-
System.out.println(
"getOriginalFilename:"+multipartfile.getOriginalFilename());
-
System.out.println(
"getName:"+multipartfile.getName());
-
// 設定格式,圖片
-
HttpHeaders headers =
new HttpHeaders();
-
headers.setContentType(MediaType.IMAGE_JPEG);
-
-
//儲存檔案到臨時目錄
-
String savePath = request.getSession().getServletContext().getRealPath(
"/")
-
+
"/uploadTempDirectory/" + multipartfile.getOriginalFilename();
-
File saveFile =
new File(savePath);
-
multipartfile.transferTo(saveFile);
-
//頁面顯示使用者上傳的圖片
-
return
new ResponseEntity<
byte[]>(FileUtils.readFileToByteArray(saveFile), headers, HttpStatus.OK);
-
}
到此基本的檔案上傳就結束了。
多檔案上傳:
多檔案上傳其實很簡單,和上傳其他相同的引數如checkbox一樣,表單中使用相同的名稱,然後action中將MultipartFile引數類定義為陣列就可以。
接下來實現:
1、建立一個上傳多檔案的表單:
-
<form action="multiUploadId" method="post" enctype="multipart/form-data">
-
<input type="file" name="idPic" />
-
<input type="file" name="idPic" />
-
<input type="file" name="idPic" />
-
<button >Submit
</button>
-
</form>
2、編寫處理表單的action:
-
<span style=
"white-space:pre"> </span>
/**
-
* 多檔案上傳
-
* @param multipartfiles
-
* @return
-
* @throws IllegalStateException
-
* @throws IOException
-
*/
-
@RequestMapping(value =
"/multiUploadId")
-
public String multiUpload(@RequestParam("idPic") MultipartFile[] multipartfiles)
-
throws IllegalStateException, IOException {
-
//儲存檔案的目錄
-
String savePath = request.getSession().getServletContext().getRealPath(
"/") +
"/uploadTempDirectory/";
-
if(
null != multipartfiles && multipartfiles.length >
0){
-
//遍歷並儲存檔案
-
for(MultipartFile file : multipartfiles){
-
file.transferTo(
new File(savePath + file.getOriginalFilename()));
-
}
-
}
-
return
"redirect:uploadPage";
-
}
執行例項,大功告成!