一、docker 安装FastDFS
1、拉取镜像
docker pull morunchang/fastdfs
2、创建并启动tracker容器
docker run -d --name=tracker -v /home/fastdfs_docker/fdfs/tracker:/data/fast_data --privileged=true --net=host morunchang/fastdfs sh tracker.sh
3、创建并启动storage容器、此处只做单机版测试
注意:由于tracker容器使用host网络模式、与宿主公用network namespace, 因此tracker容器ip与宿主机ip一致
docker run -d --name=storage -v /home/fastdfs_docker/fdfs/storage_data:/data/fast_data --privileged=rue --net=host -e TRACKER_IP=[宿主机ip]:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
二、java客户端测试
1、创建maven测试工程
pom.xml
xc-framework-parent
com.dehigher
1.0-SNAPSHOT
../xc-framework-parent/pom.xml
4.0.0
test-fastdfs
org.springframework.boot
spring-boot-starter-web
net.oschina.zcx7878
fastdfs-client-java
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-io
2、在classpath:config下创建fastdfs-client.properties文件
fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5 #http连接超时时间fastdfs.network_timeout_in_seconds= 60 #tracker 与 storage 通信连接超时时间fastdfs.charset= UTF-8 #字符编码fastdfs.tracker_servers= [tracker_server_ip]:22122 #tracker_server_ip
3、上传文件测试
/*** 上传文件
*@throwsException*/@Testpublic void testUpload() throwsException{
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 = newTrackerClient();//连接tracker Server
TrackerServer ts =tc.getConnection();if (ts == null) {
System.out.println("getConnection return null");return;
}//获取一个storage server
StorageServer ss =tc.getStoreStorage(ts);if (ss == null) {
System.out.println("getStoreStorage return null");
}//创建一个storage存储客户端
StorageClient1 sc1 = newStorageClient1(ts, ss);
NameValuePair[] meta_list= null; //new NameValuePair[0];
String item = "C:\\Users\\degao\\Pictures\\111.png";
String fileid;
fileid= sc1.upload_file1(item, "png", meta_list);
System.out.println("Upload local file " + item + " ok, fileid=" +fileid);
}
4、查询文件信息测试
/*** 查询文件
*@throwsException*/@Testpublic void testQueryFile() throwsException{
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker= newTrackerClient();
TrackerServer trackerServer=tracker.getConnection();
StorageServer storageServer= null;
StorageClient storageClient= newStorageClient(trackerServer,
storageServer);
FileInfo fileInfo= storageClient.query_file_info("group1","M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
System.out.println(fileInfo);
}
5、下载文件测试
/*** 下载文件*/@Testpublic void testDownloadFile() throwsException {
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient tracker= newTrackerClient();
TrackerServer trackerServer=tracker.getConnection();
StorageServer storageServer= null;
StorageClient1 storageClient1= newStorageClient1(trackerServer,
storageServer);byte[] result = storageClient1.download_file1("group1/M00/00/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png");
File file= new File("d:/1.png");
FileOutputStream fileOutputStream= newFileOutputStream(file);
fileOutputStream.write(result);
fileOutputStream.close();
}