Spring Boot教程(十三):Spring Boot檔案上傳
阿新 • • 發佈:2019-01-11
一、建立一個簡單的包含WEB依賴的SpringBoot專案
pom.xml
內容:
<!-- Spring Boot web啟動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp -->
<dependency>
<groupId >javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<scope>provided</scope>-->
</dependency >
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
二、配置檔案上傳的檔案大小限制
application.properties
配置檔案新增:
# 上傳檔案總的最大值
spring.servlet.multipart.max-request-size=10MB
# 單個檔案的最大值
spring.servlet.multipart.max-file-size=10MB
## jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc .view.suffix=.jsp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
spring.servlet.multipart.max-file-size
限制單個檔案的最大值spring.servlet.multipart.max-request-size
限制上傳的多個檔案的總大小
三、單檔案上傳示例
1、建立Controller控制類,內容如下:
package com.songguoliang.springboot.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* @Description
* @Author sgl
* @Date 2018-05-15 14:04
*/
@Controller
public class UploadController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
@GetMapping("/upload")
public String upload() {
return "upload";
}
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上傳失敗,請選擇檔案";
}
String fileName = file.getOriginalFilename();
String filePath = "/Users/itinypocket/workspace/temp/";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
LOGGER.info("上傳成功");
return "上傳成功";
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
return "上傳失敗!";
}
}
- 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
2、建立upload.jsp
檔案
只有一個表單,選擇檔案,form的enctype
為multipart/form-data
:
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>單檔案上傳</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
3、通過springboot外掛啟動專案,瀏覽器輸入http://localhost:8080/upload
:
選擇檔案點選提交按鈕返回成功資訊,我們上傳的檔案儲存在/Users/itinypocket/workspace/temp
路徑下:
四、多檔案上傳
1、建立多檔案上傳的jsp頁面,多檔案上傳頁面只是比單檔案上傳多了file選擇的input而已,multiUpload.jsp
內容如下:
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>多檔案上傳</title>
</head>
<body>
<form method="post" action="/multiUpload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2、在UploadController
裡新增多檔案上傳的方法
@GetMapping("/multiUpload")
public String multiUpload() {
return "multiUpload";
}
@PostMapping("/multiUpload")
@ResponseBody
public String multiUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
String filePath = "/Users/itinypocket/workspace/temp/";
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);
if (file.isEmpty()) {
return "上傳第" + (i++) + "個檔案失敗";
}
String fileName = file.getOriginalFilename();
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
LOGGER.info("第" + (i + 1) + "個檔案上傳成功");
} catch (IOException e) {
LOGGER.error(e.toString(), e);
return "上傳第" + (i++) + "個檔案失敗";
}
}
return "上傳成功";
}
- 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
3、重啟服務,瀏覽器輸入http://localhost:8080/multiUpload
:
4、然後選擇要上傳的檔案,點選提交按鈕,得到成功資訊:
我們選擇的三個檔案已被成功上傳到/Users/itinypocket/workspace/temp
路徑下。