一、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
<?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.dehigher</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> </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> </dependency> </dependencies> </project>
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、上传文件测试
/** * 上传文件 * @throws Exception */ @Test public void testUpload() throws Exception{ 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); if (ss == null) { System.out.println("getStoreStorage return null"); } //创建一个storage存储客户端 StorageClient1 sc1 = new StorageClient1(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、查询文件信息测试
/** * 查询文件 * @throws Exception */ @Test public void testQueryFile() throws Exception{ 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/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png"); System.out.println(fileInfo); }
5、下载文件测试
/** * 下载文件 */ @Test public void testDownloadFile() throws Exception { 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/00/rBsAAlwM58eAcFPfAAFs5SjEXRM444.png"); File file = new File("d:/1.png"); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(result); fileOutputStream.close(); }