Skip to content

Commit

Permalink
Spring Boot 2.x基础教程:实现文件上传
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc87112 committed Dec 30, 2020
1 parent e61e564 commit 5cb0e98
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2.x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@

- [Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面](http://blog.didispace.com/spring-boot-learning-21-4-1/)
- [Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表](http://blog.didispace.com/spring-boot-learning-21-4-2/)
- [Spring Boot 2.x基础教程:实现文件上传](http://blog.didispace.com/spring-boot-learning-21-4-3/)

## 版本资讯

Expand Down
1 change: 1 addition & 0 deletions 2.x/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

- [Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面](http://blog.didispace.com/spring-boot-learning-21-4-1/)
- [Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表](http://blog.didispace.com/spring-boot-learning-21-4-2/)
- [Spring Boot 2.x基础教程:实现文件上传](http://blog.didispace.com/spring-boot-learning-21-4-3/)

## 版本资讯

Expand Down
29 changes: 29 additions & 0 deletions 2.x/chapter4-3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

### VS Code ###
.vscode/
53 changes: 53 additions & 0 deletions 2.x/chapter4-3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.didispace</groupId>
<artifactId>chapter4-3</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.didispace.chapter43;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Chapter43Application {

public static void main(String[] args) {
SpringApplication.run(Chapter43Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.didispace.chapter43;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@Slf4j
public class UploadController {

@Value("${file.upload.path}")
private String path;

@GetMapping("/")
public String uploadPage() {
return "upload";
}

@PostMapping("/upload")
@ResponseBody
public String create(@RequestPart MultipartFile file) {

String fileName = file.getOriginalFilename();
String filePath = path + fileName;

String message;
File dest = new File(filePath);
try {
file.transferTo(dest);
message = "Upload file success : " + dest.getAbsolutePath();
} catch (IOException e) {
log.error(e.getMessage(), e);
message = "Upload file failed : " + e.getMessage();
}
return message;
}

}
5 changes: 5 additions & 0 deletions 2.x/chapter4-3/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB

file.upload.path=/Users/didi/
15 changes: 15 additions & 0 deletions 2.x/chapter4-3/src/main/resources/templates/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>文件上传页面</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
选择要上传的文件:<input type="file" name="file"><br>
<hr>
<input type="submit" value="提交">
</form>
</body>
</html>
11 changes: 11 additions & 0 deletions 2.x/chapter4-3/src/test/java/FileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.junit.jupiter.api.Test;

public class FileTest {

@Test
public void uploadFile() throws Exception {


}

}
1 change: 1 addition & 0 deletions 2.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<!-- Web开发 -->
<module>chapter4-1</module> <!-- 使用 Thymeleaf开发Web页面 -->
<module>chapter4-2</module> <!-- 使用 ECharts 绘制折线图 -->
<module>chapter4-3</module> <!-- 文件的上传与下载 -->

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

0 comments on commit 5cb0e98

Please sign in to comment.