二、fastDFS入门
2.1fastDFS安装
tracker和storage使用相同的安装包,fastDFS的下载地址在:https://github.com/happyfish100/FastDFS,FastDFS是C语言开发,建议在linux上运行。安装细节本篇博文就不做赘述了,感兴趣的朋友可以评论区留言我会单独写一篇博文来介绍fastDFS在Linux中的安装和配置。
2.2文件上传下载测试
2.2.1搭建环境
这里我们使用javaApi测试文件的上传,java版本的fastdfs-client地址在:https://github.com/happyfish100/fastdfs-client-java,参考此工程编写测试用例。
此工程采用SpringBoot创建,导入资料下边的test-fastdfs.zip,下边是一些配置文件介绍:
1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xc-framework-parent</artifactId>
<groupId>com.fastDFS</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../xc-framework-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-fastdfs</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
2) 配置文件
在classpath:config下创建fastdfs-client.properties文件
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 192.168.101.65:22122
fastdfs.connect_timeout_in_seconds: #http连接超时时间
fastdfs.network_timeout_in_seconds: #tracker与storage网络通信超时时间
fastdfs.charset:#字符编码
fastdfs.tracker_servers:tracker#服务器地址,多个地址中间用英文逗号分隔
2.2.2 文件上传
//上传文件
@Test
public void testUpload() {
try {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
//创建客户端
TrackerClient tc = new TrackerClient();
//连接tracker Server
TrackerServer ts = tc.getConnection();
if (ts == null) {
System.out.println("getConnection return null");
return;
}
//获取一个storage server
StorageServer ss = tc.getStoreStorage(ts);
//创建一个storage存储客户端
StorageClient1 sc1 = new StorageClient1(ts, ss);
//本地文件路径
String item = "C:\\Users\\admin\\Desktop\\1.png";
String fileid;
fileid = sc1.upload_file1(item, "png", null);
System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
} catch (Exception ex) {
ex.printStackTrace();
}
}
2.2.3 文件查询
//查询文件
@Test
public void testQueryFile() throws IOException, MyException {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
FileInfo fileInfo = storageClient.query_file_info("group1", "M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
System.out.println(fileInfo);
}
2.2.4 文件下载
//下载文件
@Test
public void testDownloadFile() throws IOException, MyException {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer,
storageServer);
byte[] result = storageClient1.download_file1("group1/M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
File file = new File("d:/1.png");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(result);
fileOutputStream.close();
}