Skip to content

Commit bb70a13

Browse files
committed
Spring Boot 2.x基础教程:实现多文件上传
1 parent 5cb0e98 commit bb70a13

10 files changed

Lines changed: 179 additions & 1 deletion

File tree

2.x/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
- [Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面](http://blog.didispace.com/spring-boot-learning-21-4-1/)
103103
- [Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表](http://blog.didispace.com/spring-boot-learning-21-4-2/)
104104
- [Spring Boot 2.x基础教程:实现文件上传](http://blog.didispace.com/spring-boot-learning-21-4-3/)
105+
- [Spring Boot 2.x基础教程:实现多文件上传](http://blog.didispace.com/spring-boot-learning-21-4-4/)
105106

106107
## 版本资讯
107108

2.x/README_zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
- [Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面](http://blog.didispace.com/spring-boot-learning-21-4-1/)
104104
- [Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表](http://blog.didispace.com/spring-boot-learning-21-4-2/)
105105
- [Spring Boot 2.x基础教程:实现文件上传](http://blog.didispace.com/spring-boot-learning-21-4-3/)
106+
- [Spring Boot 2.x基础教程:实现多文件上传](http://blog.didispace.com/spring-boot-learning-21-4-4/)
106107

107108
## 版本资讯
108109

2.x/chapter4-4/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.x/chapter4-4/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.4.1</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter4-4</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<scope>provided</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace.chapter44;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Chapter44Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Chapter44Application.class, args);
11+
}
12+
13+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace.chapter44;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestPart;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.multipart.MultipartFile;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
@Controller
16+
@Slf4j
17+
public class UploadController {
18+
19+
@Value("${file.upload.path}")
20+
private String path;
21+
22+
@GetMapping("/")
23+
public String uploadPage() {
24+
return "upload";
25+
}
26+
27+
@PostMapping("/upload")
28+
@ResponseBody
29+
public String create(@RequestPart MultipartFile[] files) {
30+
StringBuffer message = new StringBuffer();
31+
32+
for(MultipartFile file : files) {
33+
String fileName = file.getOriginalFilename();
34+
String filePath = path + fileName;
35+
36+
File dest = new File(filePath);
37+
try {
38+
file.transferTo(dest);
39+
message.append("Upload file success : " + dest.getAbsolutePath()).append("<br>");
40+
} catch (IOException e) {
41+
log.error(e.getMessage(), e);
42+
message.append("Upload file failed : " + e.getMessage()).append("<br>");
43+
}
44+
}
45+
return message.toString();
46+
}
47+
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
spring.servlet.multipart.max-file-size=2MB
3+
spring.servlet.multipart.max-request-size=2MB
4+
5+
file.upload.path=/Users/didi/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8" />
5+
<title>文件上传页面</title>
6+
</head>
7+
<body>
8+
<h1>文件上传页面</h1>
9+
<form method="post" action="/upload" enctype="multipart/form-data">
10+
文件1:<input type="file" name="files"><br>
11+
文件2:<input type="file" name="files"><br>
12+
<hr>
13+
<input type="submit" value="提交">
14+
</form>
15+
</body>
16+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
public class FileTest {
4+
5+
@Test
6+
public void uploadFile() throws Exception {
7+
8+
9+
}
10+
11+
}

2.x/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
<!-- Web开发 -->
4545
<module>chapter4-1</module> <!-- 使用 Thymeleaf开发Web页面 -->
4646
<module>chapter4-2</module> <!-- 使用 ECharts 绘制折线图 -->
47-
<module>chapter4-3</module> <!-- 文件的上传与下载 -->
47+
<module>chapter4-3</module> <!-- 实现文件上传 -->
48+
<module>chapter4-4</module> <!-- 实现多文件上传 -->
4849

4950
<!-- 各种缓存 -->
5051
<module>chapter5-1</module> <!-- 使用进程内缓存 -->

0 commit comments

Comments
 (0)