java使用fastdfs

前置

安装了maven

使用fastdfs-client-java

https://github.com/happyfish100/fastdfs-client-java

在maven项目pom.xml中添加依赖

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

一般下不下来:

可以去github: https://github.com/happyfish100/fastdfs-client-java/archive/refs/tags/V1.28.tar.gz下载

并mvn clean install 安装到本地仓库。

加载配置

配置文件两种conf和properties

把配置文件放到classpath目录中:fdfs_client.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122

connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
1
2
加载原 conf 格式文件配置:
ClientGlobal.init("fdfs_client.conf");

也可以是properties : fastdfs-client.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80

fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122

fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000
1
2
加载 properties 格式文件配置:
ClientGlobal.initByProperties("fastdfs-client.properties");

检测加载配置:

1
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());

上传和下载

FdfsTest.java

使用FastDFS_Client

https://github.com/tobato/FastDFS_Client

在spring boot 环境中使用:

Pom.xml

1
2
3
4
5
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>

Application.yml

1
2
3
4
5
6
7
8
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.158.128:22122

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   @GetMapping("/test")
public void test() throws FileNotFoundException {
apiService.commonmerMerBind();
File file = new File("/Users/qichuanhan/Downloads/install.sh");
InputStream inputStream = new FileInputStream(file);
StorePath storePath = storageClient.uploadFile("group1", inputStream,
file.length(), "sh");
log.info(storePath.getFullPath());
// group1/M00/00/00/wKiegGJ2QZuAGO_pAAB0ILgQ4BQ9615.sh

}

@RequestMapping("/down")
@ResponseBody
public ResponseEntity<byte[]> down(HttpServletResponse resp) {

DownloadByteArray cb = new DownloadByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "install.sh");
byte[] bs = storageClient.downloadFile("group1", "M00/00/00/wKiegGJ2QZuAGO_pAAB0ILgQ4BQ9615.sh", cb);

return new ResponseEntity<>(bs,headers, HttpStatus.OK);
}

java使用fastdfs
http://hanqichuan.com/2022/05/06/fastDFS/java使用fastDfs/
作者
韩启川
发布于
2022年5月6日
许可协议