准备docker环境(略)
编写docker-compose.yml
version: '3'
services:
fastdfs:
image: registry.cn-hangzhou.aliyuncs.com/qbanxiaoli/fastdfs
container_name: fastdfs-single
environment:
# nginx服务端口,默认80端口,可修改
WEB_PORT: 9080
# tracker_server服务端口,默认22122端口,可修改
FDFS_PORT: 22122
# fastdht服务端口,默认11411端口,可修改
FDHT_PORT: 11411
# docker所在的宿主机内网地址,默认使用eth0网卡的地址
IP: 192.168.31.163
volumes:
# 将本地目录映射到docker容器内的fastdfs数据存储目录,将fastdfs文件存储到主机上,以免每次重建docker容器,之前存储的文件就丢失了。
- ./fastdfs:/var/local
# 网络模式为host,可不暴露端口,即直接使用宿主机的网络端口,只适用于linux系统
network_mode: host
引入依赖配置
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>${fastdfs-version}</version>
</dependency>
编写测试代码
fdfs:
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.31.163:22122
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class FastFileStorageClientTest {
@Autowired
private FastFileStorageClient storageClient;
@Test
public void testUploadFile() throws IOException {
//URL testResource = FastFileStorageClientTest.class.getResource("/application.yml");
//log.info(testResource.toString());
Resource testResource = new ClassPathResource("/application.yml");
StorePath storePath = storageClient.uploadFile(new FastFile.Builder()
.withFile(testResource.getInputStream(), testResource.contentLength(), "yml")
.build()
);
log.info(storePath.getFullPath());
}
}
更多使用案例
https://github.com/tobato/FastDFS_Client/tree/master/src/test/java/com/github/tobato/fastdfs/service