Skip to content

Commit 0b48c15

Browse files
committed
新增SpringBoot整合FastDFS上传文件到文件服务器
1 parent ee6ec86 commit 0b48c15

9 files changed

Lines changed: 309 additions & 0 deletions

File tree

Spring-Boot-FastDFS/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.5.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>fastdfs</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>Spring-Boot-FastDFS</name>
15+
<description>Demo project for Spring Boot</description>
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>com.github.tobato</groupId>
28+
<artifactId>fastdfs-client</artifactId>
29+
<version>1.26.7</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
<exclusions>
40+
<exclusion>
41+
<groupId>org.junit.vintage</groupId>
42+
<artifactId>junit-vintage-engine</artifactId>
43+
</exclusion>
44+
</exclusions>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.fastdfs;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootFastDfsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootFastDfsApplication.class, args);
11+
}
12+
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.fastdfs.bean;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* @author dengzhiming
8+
* @date 2020/3/6 18:09
9+
*/
10+
@Component
11+
public class FastDFSProperties {
12+
13+
@Value("${fdfs.resHost}")
14+
private String resHost;
15+
16+
@Value("${fdfs.storagePort}")
17+
private String storagePort;
18+
19+
public String getResHost() {
20+
return resHost;
21+
}
22+
23+
public void setResHost(String resHost) {
24+
this.resHost = resHost;
25+
}
26+
27+
public String getStoragePort() {
28+
return storagePort;
29+
}
30+
31+
public void setStoragePort(String storagePort) {
32+
this.storagePort = storagePort;
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.example.fastdfs.controller;
2+
3+
import com.example.fastdfs.util.FastDFSUtil;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.multipart.MultipartFile;
12+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* @author dengzhiming
18+
* @date 2020/3/6 18:12
19+
*/
20+
@Controller
21+
public class TestController {
22+
23+
private static Logger logger = LoggerFactory.getLogger(TestController.class);
24+
@Autowired
25+
private FastDFSUtil dfsClient;
26+
27+
@GetMapping("/")
28+
public String index() {
29+
return "upload";
30+
}
31+
32+
@PostMapping("/upload")
33+
public String fileUpload(@RequestParam("file") MultipartFile multipartFile
34+
,RedirectAttributes redirectAttributes){
35+
if (multipartFile.isEmpty()) {
36+
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
37+
return "redirect:uploadStatus";
38+
}
39+
// 将图片或者音视频上传到分布式的文件存储系统
40+
// 将图片的存储路径返回给页面
41+
try {
42+
String path = dfsClient.uploadFile(multipartFile);
43+
redirectAttributes.addFlashAttribute("message",
44+
"You successfully uploaded '" + multipartFile.getOriginalFilename() + "'");
45+
redirectAttributes.addFlashAttribute("path",
46+
"file path url '" + path + "'");
47+
} catch (IOException e) {
48+
logger.error("upload file failed",e);
49+
}
50+
return "redirect:/uploadStatus";
51+
}
52+
53+
@GetMapping("/uploadStatus")
54+
public String uploadStatus() {
55+
return "uploadStatus";
56+
}
57+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.example.fastdfs.util;
2+
3+
import com.example.fastdfs.bean.FastDFSProperties;
4+
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
5+
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
6+
import com.github.tobato.fastdfs.service.FastFileStorageClient;
7+
import org.apache.commons.io.FilenameUtils;
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.web.multipart.MultipartFile;
13+
14+
import javax.annotation.Resource;
15+
import java.io.ByteArrayInputStream;
16+
import java.io.IOException;
17+
import java.nio.charset.StandardCharsets;
18+
19+
/**
20+
* @author dengzhiming
21+
* @date 2020/3/6 18:15
22+
*/
23+
@Component
24+
public class FastDFSUtil {
25+
private Logger logger = LoggerFactory.getLogger(this.getClass());
26+
27+
@Resource
28+
private FastFileStorageClient storageClient;
29+
// 项目参数配置
30+
@Resource
31+
private FastDFSProperties appConfig;
32+
33+
//上传文件
34+
public String uploadFile(MultipartFile file) throws IOException {
35+
long startTime = System.currentTimeMillis();
36+
// 获得文件后缀名
37+
StorePath storePath = storageClient.uploadFile(
38+
file.getInputStream(),
39+
file.getSize(),
40+
FilenameUtils.getExtension(file.getOriginalFilename()),
41+
null);
42+
logger.info("the time for uploading file is " + (System.currentTimeMillis() - startTime) + " ms");
43+
return getResAccessUrl(storePath);
44+
}
45+
46+
//将一段字符串生成一个文件上传
47+
public String uploadFile(String content, String fileExtension) {
48+
byte[] buff = content.getBytes(StandardCharsets.UTF_8);
49+
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
50+
StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);
51+
return getResAccessUrl(storePath);
52+
}
53+
54+
// 封装图片完整URL地址
55+
private String getResAccessUrl(StorePath storePath) {
56+
String fileUrl = "http://" + appConfig.getResHost()
57+
+ ":" + appConfig.getStoragePort() + "/" + storePath.getFullPath();
58+
return fileUrl;
59+
}
60+
61+
//删除文件
62+
public void deleteFile(String fileUrl) {
63+
if (StringUtils.isEmpty(fileUrl)) {
64+
return;
65+
}
66+
try {
67+
StorePath storePath = StorePath.parseFromUrl(fileUrl);
68+
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
69+
} catch (FdfsUnsupportStorePathException e) {
70+
logger.warn(e.getMessage());
71+
}
72+
}
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# thymeleaf
2+
spring:
3+
thymeleaf:
4+
prefix: classpath:/templates/
5+
suffix: .html
6+
mode: HTML
7+
encoding: UTF-8
8+
servlet:
9+
content-type: text/html
10+
cache: false
11+
servlet:
12+
multipart:
13+
max-request-size: 100MB
14+
max-file-size: 10MB
15+
16+
# FastDFS\u56E0\u5B89\u88C5\u5728linux\u4E0A,\u9700\u8981\u5F00\u901ANginx\u76D1\u63A7\u7AEF\u53E380,tracker\u670D\u52A1\u5668\u7AEF\u53E322122,storage\u670D\u52A1\u5668\u7AEF\u53E323000
17+
fdfs:
18+
#\u8FDE\u63A5tracker\u670D\u52A1\u5668\u8D85\u65F6\u65F6\u957F
19+
connect-timeout: 6000
20+
#socket\u8FDE\u63A5\u8D85\u65F6\u65F6\u957F
21+
so-timeout: 6000
22+
#FastDFS\u670D\u52A1\u5668\u5730\u5740
23+
resHost: 47.92.132.50
24+
#Nginx\u76D1\u542C\u7AEF\u53E3
25+
storagePort: 80
26+
#\u7F29\u7565\u56FE\u751F\u6210\u53C2\u6570,\u53EF\u9009
27+
#thumb-image:
28+
#width: 150
29+
#height: 150
30+
#Tracker\u670D\u52A1\u5668\u5217\u8868,\u53EF\u591A\u4E2A
31+
tracker-list:
32+
- 47.92.132.50:22122
33+
pool:
34+
#\u6700\u5927\u8FDE\u63A5\u6570
35+
max-total: 150
36+
#\u83B7\u53D6\u8FDE\u63A5\u65F6\u7684\u6700\u5927\u7B49\u5F85\u6BEB\u79D2\u6570
37+
max-wait-millis: 5000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<h1>Spring Boot file upload example</h1>
5+
6+
<form method="POST" action="/upload" enctype="multipart/form-data">
7+
<input type="file" name="file" /><br/><br/>
8+
<input type="submit" value="Submit" />
9+
</form>
10+
</body>
11+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<body>
4+
<h1>Spring Boot - Upload Status</h1>
5+
6+
<div th:if="${message}">
7+
<h2 th:text="${message}"></h2>
8+
</div>
9+
10+
<div th:if="${path}">
11+
<h2 th:text="${path}"></h2>
12+
</div>
13+
</body>
14+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.fastdfs;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringBootFastDfsApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)