JAVA 访问fastdfs

官网

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

依赖


    <dependencies>
        <!--fastdfs的java客户端-->
        <dependency>
            <groupId>cn.bestwu</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

配置文件fastdfs-client.properties

在这里插入图片描述

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 192.168.181.135:22122

测试

package com.liu;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FastDFSTest {
    
    


    @Test
    public void testUpload(){
    
    


        try {
    
    
            //1.加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //2.创建tracker客户端
            TrackerClient tc = new TrackerClient();
            //根据tracker客户端创建连接 获取到跟踪服务器对象
            TrackerServer ts = tc.getConnection();
            //存储服务端
            StorageServer ss=null;

            //定义StorageClient
            StorageClient1 client1 = new StorageClient1(ts,ss);

            //文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", "1.png");

            String fileId =  client1.upload_file1("E:\\图\\timg (1).jpg", "jpg", list);

            System.out.println(fileId);


        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (MyException e) {
    
    
            e.printStackTrace();
        }


    }
    //group1/M00/00/00/wKi1h190QBeAPbZdAAGiZY4fIE4871.png
    //group1/M00/00/00/wKi1h190QEiAbyCqAAGiZY4fIE4965.jpg

    @Test
    public  void  testQuery(){
    
    
        try {
    
    
            // 加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            // 根据tracker客户端端获取连接  获取跟踪服务器对象
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            // 定义存储客户端
            StorageClient1 client1 = new StorageClient1(trackerServer,storageServer);

            FileInfo fileInfo = client1.query_file_info1("group1/M00/00/00/wKi1h190QEiAbyCqAAGiZY4fIE4965.jpg");
            NameValuePair[] nameValuePairs = client1.get_metadata1("group1/M00/00/00/wKi1h190QEiAbyCqAAGiZY4fIE4965.jpg");
            System.out.println(nameValuePairs);
            if(nameValuePairs != null){
    
    
                for (int i=0;i<nameValuePairs.length;i++){
    
    
                    System.out.println(nameValuePairs[i].getName() + ":"+nameValuePairs[i].getValue());
                }
            }
            System.out.println(fileInfo);
            //group1/M00/00/00/wKi1h190QBeAPbZdAAGiZY4fIE4871.png
            //group1/M00/00/00/wKi1h190QEiAbyCqAAGiZY4fIE4965.jpg
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (MyException e) {
    
    
            e.printStackTrace();
        }
    }
    @Test
    public  void  testDownload(){
    
    
        try {
    
    
            // 加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            // 根据tracker客户端端获取连接  获取跟踪服务器对象
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            // 定义存储客户端
            StorageClient1 client1 = new StorageClient1(trackerServer,storageServer);
            byte[] bs = client1.download_file1("group1/M00/00/00/wKi1h190QEiAbyCqAAGiZY4fIE4965.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream(new File("download1.png"));
            fileOutputStream.write(bs);
            fileOutputStream.close();

        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (MyException e) {
    
    
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ko0491/article/details/108886543