1
|
$ docker pull delron/fastdfs
|
1
2
3
4
5
|
$ docker run -d --name fastdfs-tracker \
--restart=always \
--network=host \
-p 22122:22122 \
delron/fastdfs tracker
|
1
2
3
4
5
6
7
8
9
10
|
mkdir -p /data/fastdfs/storage
docker run -d --name fastdfs-storage \
--link fastdfs-tracker:tracker \
--restart=always \
--network=host \
-e TRACKER_SERVER=tracker:22122 \
-v /data/fastdfs/storage:/var/fdfs \
-p 23000:23000 -p 8080:8080 -p 8888:8888 \
delron/fastdfs storage
|
1
2
3
|
$ docker exec -it fastdfs-storage /bin/bash
cd /etc/fdfs/
vi storage.conf
|
1
2
3
|
firewall-cmd --zone=public --permanent --add-port=8888/tcp
firewall-cmd --zone=public --permanent --add-port=22122/tcp
firewall-cmd --zone=public --permanent --add-port=23000/tcp
|
1
2
|
# systemctl restart firewalld
$ sudo docker ps
|
1
2
3
4
5
|
$ docker exec -it fastdfs-storage /bin/bash
cd /var/fdfs/
echo hello testdoc>b.txt
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf b.txt
group1/M00/00/00/rBEABWdHM8iAGXyhAAAAFy0dlFI076.txt
|
Accessed through a browser
Maven References
1
2
3
4
5
|
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
|
Specify the FastDFS configuration in the project’s configuration file, application.properties
1
2
3
4
5
6
7
|
fdfs.so-timeout=1500
fdfs.connect-timeout=600
#thumbImage param
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
#TrackerList参数,支持多个
fdfs.tracker-list=192.168.157.129:22122
|
File upload and download
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
|
package com.lm.shop.shopeureka.util;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@Component
public class FastDFSUtil {
private final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);
@Autowired
private FastFileStorageClient fileStorageClient;
/**
* 文件上传
*
* @param multipartFile 附件上传
* @return fastDfs路径
*/
public String uploadFile(MultipartFile multipartFile) throws Exception{
String originalFilename = multipartFile.getOriginalFilename().
substring(multipartFile.getOriginalFilename().
lastIndexOf(".") + 1);
StorePath storePath = this.fileStorageClient.uploadImageAndCrtThumbImage(
multipartFile.getInputStream(),
multipartFile.getSize(),originalFilename , null);
return storePath.getFullPath() ;
}
/**
* 删除文件
*
* @param filePath 附件路径
* @return void
*/
public void delFile(String filePath) {
this.fileStorageClient.deleteFile(filePath);
}
/**
* 文件上传
*
* @param bytes 文件字节
* @param fileSize 文件大小
* @param extension 文件扩展名
* @return fastDfs路径
*/ public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StorePath storePath = fileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
logger.info(storePath.getGroup() + "===" + storePath.getPath() + "======" + storePath.getFullPath());
return storePath.getFullPath();
}
/**
* 下载文件
*
* @param fileUrl 文件URL
* @return 文件字节
* @throws IOException
*/
public byte[] downloadFile(String fileUrl) throws IOException {
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
}
|