FastDFS实战(四)- Java集成FastDFS

一、 下载 https://github.com/happyfish100/fastdfs-client-java.git
二、解压缩 安装到本地仓库

这里写图片描述

<!-- fastdfs java依赖 -->
<dependency>
   <groupId>org.csource</groupId>
   <artifactId>fastdfs-client-java</artifactId>
   <version>1.27-SNAPSHOT</version>
</dependency>
三、准备配置文件 fdfs_client.conf 放置在src下
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

tracker_server = 192.168.128.141:22122

注1:tracker_server指向您自己IP地址和端口,1-n个
注2:除了tracker_server,其它配置项都是可选的

四、测试
  1. 测试上传

    private String conf_filename="fdfs_client.conf";
    @Test
    public void testUpload(){
       try {
           ClientGlobal.init(conf_filename);
    
           TrackerClient client = new TrackerClient();
    
           TrackerServer server = client.getConnection();
    
           StorageClient storageClient = new StorageClient(server, null);
    
           NameValuePair[] nameValuePairs = new NameValuePair[]{
                   new NameValuePair("id","100"),
                   new NameValuePair("name","小红")
    
           };
           String[] file = storageClient.upload_file("d:\\c.jpg", "jpg", nameValuePairs);
    
           for (String s : file) {
               System.out.println(s);
           }
    
       } catch (IOException e) {
           e.printStackTrace();
       } catch (MyException e) {
           e.printStackTrace();
       }
    }

    这里写图片描述

  2. 测试下载

    @Test
    public void testDownLoad(){
        try {
            ClientGlobal.init(conf_filename);
    
            TrackerServer trackerServer = new TrackerClient().getConnection();
    
            StorageClient storageClient = new StorageClient(trackerServer, null);
    
            byte[] bytes = storageClient.download_file("group1", "M00/00/00/wKiAjVlpGguASKnyAADQcgmZjMs583.jpg");
    
            FileOutputStream stream = new FileOutputStream("test.jpg");
    
            stream.write(bytes);
    
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
  3. 测试删除

    @Test
    public void testDelete(){
       try {
           ClientGlobal.init(conf_filename);
    
           TrackerServer trackerServer = new TrackerClient().getConnection();
    
           StorageClient storageClient = new StorageClient(trackerServer, null);
    
           int i= storageClient.delete_file("group1", "M00/00/00/wKiAjVlpGguASKnyAADQcgmZjMs583.jpg");
    
       } catch (IOException e) {
           e.printStackTrace();
       } catch (MyException e) {
           e.printStackTrace();
       }
    }
  4. 测试获取元数据

    @Test
    public void testGetMetaData(){
        try {
            ClientGlobal.init(conf_filename);
    
            TrackerServer trackerServer = new TrackerClient().getConnection();
    
            StorageClient storageClient = new StorageClient(trackerServer, null);
    
            NameValuePair[] metadata = storageClient.get_metadata("group1", "M00/00/00/wKiAjVlpHBuAJE_yAADQcgmZjMs320.jpg");
    
            for (NameValuePair metadatum : metadata) {
                System.out.println(metadatum.getName()+"  "+metadatum.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
  5. 测试获取文件信息

    @Test
    public void testGetFileInfo(){
        try {
            ClientGlobal.init(conf_filename);
    
            TrackerServer trackerServer = new TrackerClient().getConnection();
    
            StorageClient storageClient = new StorageClient(trackerServer, null);
    
            FileInfo fileInfo = storageClient.get_file_info("group1", "M00/00/00/wKiAiVlkVcqALRgEAAAABudnRxc478.txt");
    
            System.out.println(fileInfo.getSourceIpAddr()+"  "+fileInfo.getCreateTimestamp()+"  "+fileInfo.getFileSize()+"  "+fileInfo.getCrc32());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值