Spring 上傳文件(轉)

Spring 上傳文件

SpringMVC 中使用 commons-fileupload 上傳文件,需要在 SpringMVC 的配置文件裏先配置 multipartResolver,然後就可以使用 MultipartFile 讀取 HTTP 請求中的文件流並保存到本地了。

Gradle 依賴

1
"commons-fileupload:commons-fileupload:1.3.1"

配置 multipartResolver

需要在 SpringMVC 的配置文件中配置 multipartResolver 用於上傳文件。

1
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

上傳單個文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.xtuer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@Controller
public class UploadController {
@Autowired
private ServletContext servletContext;
/**
* 上傳單個文件的頁面
* @return 頁面的路徑
*/
@RequestMapping(value = "/upload-file", method = RequestMethod.GET)
public String uploadFilePage() {
return "upload-file.html";
}
/**
* 上傳單個文件
*
* @param file 上傳文件 MultipartFile 的對象
* @return 上傳的結果
*/
@RequestMapping(value = "/upload-file", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file) {
saveFile(file);
return "Success";
}
/**
* 把 HTTP 請求中的文件流保存到本地
*
* @param file MultipartFile 的對象
*/
private boolean saveFile(MultipartFile file) {
if (!file.isEmpty()) {
try {
// getRealPath() 取得 WEB-INF 所在文件夾路徑
// 如果參數是 "/temp", 當 temp 存在時返回 temp 的本地路徑, 不存在時返回 null/temp (無效路徑)
String path = servletContext.getRealPath("") + File.separator + file.getOriginalFilename();
FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(path));
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
}

upload-file.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="padding: 50px">
選擇文件
<form action="/upload-file" method="post" enctype="multipart/form-data">
<input type="file" name="file"/><br>
<button type="submit">上傳</button>
</form>
</body>
</html>

測試上傳單個文件

  1. 訪問 http://localhost:8080/upload-file
  2. 選擇文件,點擊 上傳 按鈕
  3. 在 WEB-INF 所在目錄中查看文件是否已經上傳好了

上傳多個文件

上傳多個文件和上傳單個文件沒多大區別,不通的只是映射函數中的參數不是 MultipartFile,而是 MultipartFile 的數組

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 上傳多個文件的頁面
* @return 頁面的路徑
*/
@RequestMapping(value = "/upload-files", method = RequestMethod.GET)
public String uploadFilesPage() {
return "upload-files.html";
}
/**
* 上傳多個文件
*
* @param files 上傳文件 MultipartFile 的對象數組
* @return 上傳的結果
*/
@RequestMapping(value = "/upload-files", method = RequestMethod.POST)
@ResponseBody
public String uploadFiles(@RequestParam("files") MultipartFile[] files) {
for (MultipartFile file : files) {
saveFile(file);
}
return "Success";
}

upload-files.html

表單中也只是多了幾個 <input type="file" name="file"/>,名字都是 file,這樣表單提交時多個同名的參數就表示數組。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="padding: 50px">
選擇文件
<form action="/upload-files" method="post" enctype="multipart/form-data">
<input type="file" name="files"/><br>
<input type="file" name="files"/><br>
<input type="file" name="files"/><br>
<button type="submit">上傳</button>
</form>
</body>
</html>

測試上傳多個文件

  1. 訪問 http://localhost:8080/upload-files
  2. 選擇多個文件,點擊 上傳 按鈕
  3. 在 WEB-INF 所在目錄中查看文件是否已經上傳好了
轉自:https://xtuer.github.io/spring-mvc-upload-file/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章