Fork me on GitHub

SpringBoot2.x整合FastDFS

目录

本篇博客学习SpringBoot 2.1.11.RELEASE整合FastDFS。

FastDFS作用

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件上传、文件下载等,解决了大容量存储和负载均衡的问题。

安装连接:

CentOS 7 安裝FastDFS V6.0.3

我们开始吧

新建一个springboot项目

pom文件

加入fastdfs-client-java包

1
2
3
4
5
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>

注意:fastdfs-client-java包mvn库中没有编译包,需要自己下载编译到自己的mvn本地库中,官方地址:https://github.com/happyfish100/fastdfs-client-java

fdfs_client.conf

resources文件夹下新建fdfs_client.conf,编写如下:

1
2
3
4
5
6
7
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 6666
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.31.100:22122

文件工具类

编写fastdfs初始化连接配置工具类

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class FastDFSService {

FastDFSService() throws IOException, MyException {
ClientGlobal.init("fdfs_client.conf");
}



public String upload(byte[] bs, String stringbe) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
String fileIds = null;
try {
trackerServer = init();
StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
fileIds = storageClient.upload_file1(bs, getFileExt(stringbe), null);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
close(storageServer, trackerServer);
}
return fileIds;
}

public byte[] download(String groupName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
byte[] b = null;
try {
trackerServer = init();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
b = storageClient1.download_file1(groupName);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(storageServer, trackerServer);
}
return b;
}

public FileInfo getFileInfo(String groupName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
FileInfo fi = null;
try {
trackerServer = init();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
fi = storageClient1.get_file_info1(groupName);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(storageServer, trackerServer);
}
return fi;
}

public NameValuePair[] getFileMate(String groupName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
NameValuePair nvps[] = null;
try {
trackerServer = init();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
nvps = storageClient1.get_metadata1(groupName);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(storageServer, trackerServer);
}
return nvps;
}

public int delete(String groupName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
int i = 0;
try {
trackerServer = init();
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
i = storageClient1.delete_file1(groupName);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(storageServer, trackerServer);
}
return i;
}

public byte[] File2byte(File file) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}

private TrackerServer init() {
TrackerServer trackerServer = null;
try {
TrackerClient tracker = new TrackerClient();
trackerServer = tracker.getConnection();
} catch (IOException e) {
e.printStackTrace();
}
return trackerServer;
}

private void close(StorageServer storageServer, TrackerServer trackerServer) {
try {
if (storageServer != null) {
storageServer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (trackerServer != null) {
trackerServer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

private String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点

}
}

public String getFileName(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains("/")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf("/") + 1); // 不带最后的点

}
}
}

演示案例

编写测试类

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
import cn.cicoding.service.FastDFSService;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootFastdfsApplicationTests {

@Autowired
private FastDFSService fastDFSService;

@Test
public void contextLoads() throws IOException {
String local_filename = "C:\\Users\\Public\\Pictures\\Sample Pictures\\123.jpg";
File f=new File(local_filename);
String groupName= fastDFSService.upload(fastDFSService.File2byte(f),f.getName());
System.out.println(groupName);
IOUtils.write(fastDFSService.download(groupName), new FileOutputStream("D:/app/fastdfs/"+fastDFSService.getFileName(groupName)));
System.out.println(fastDFSService.getFileInfo(groupName));
System.out.println(fastDFSService.getFileMate(groupName));
System.out.println(fastDFSService.delete(groupName)==0 ? "删除成功" : "删除失败");

}

}

运行测试类得到结果:

1
2
3
4
group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg
123
.jpg
删除成功

源码可以加群获取!

相关文章

微信打赏

赞赏是不耍流氓的鼓励

评论系统未开启,无法评论!