博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastDfs java客户端上传、删除文件
阅读量:5098 次
发布时间:2019-06-13

本文共 5248 字,大约阅读时间需要 17 分钟。

#配置文件

connect_timeout = 2

 

network_timeout = 30

 

charset = UTF-8

 

http.tracker_http_port = 9090

 

http.anti_steal_token = no

 

http.secret_key = FastDFS1234567890

 

tracker_server = 192.168.248.128:22122

 

#tracker_server = 192.168.0.119:22122

 
package com.enation.newtest;import java.io.IOException;import org.junit.Test;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.UUID;import org.apache.commons.io.IOUtils;import org.csource.common.MyException;import org.csource.common.NameValuePair;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.FileInfo;import org.csource.fastdfs.StorageClient;import org.csource.fastdfs.StorageClient1;import org.csource.fastdfs.StorageServer;import org.csource.fastdfs.TrackerClient;import org.csource.fastdfs.TrackerServer;import org.junit.After;import org.junit.Before;public class TestFastDfs {        public String conf_filename = "E:\\fdfs_client.conf";     //public String local_filename = "D:\\stsworkspace\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf";        public String local_filename = "E:\\2.jpg";    @Before    public void setUp() throws Exception {    }    @After    public void tearDown() throws Exception {    }    @Test    public void testUpload() {        try {             ClientGlobal.init(conf_filename);            TrackerClient tracker = new TrackerClient();             TrackerServer trackerServer = tracker.getConnection();             StorageServer storageServer = null;            StorageClient1 client = new StorageClient1(trackerServer, storageServer);                         StorageClient storageClient = new StorageClient(trackerServer, storageServer); //          NameValuePair nvp = new NameValuePair("age", "18");             NameValuePair nvp [] = new NameValuePair[]{                     new NameValuePair("age", "18"),                     new NameValuePair("sex", "male")             };             String fileIds[] = storageClient.upload_file(local_filename, "jpg", nvp);                        System.out.println(fileIds.length);             System.out.println("组名:" + fileIds[0]);             System.out.println("路径: " + fileIds[1]);        } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } catch (MyException e) {             e.printStackTrace();         }     }    @Test     public void testDownload() {        try {            ClientGlobal.init(conf_filename);            TrackerClient tracker = new TrackerClient();             TrackerServer trackerServer = tracker.getConnection();             StorageServer storageServer = null;            StorageClient storageClient = new StorageClient(trackerServer, storageServer);             byte[] b = storageClient.download_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             System.out.println(b);             IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".conf"));        } catch (Exception e) {             e.printStackTrace();         }     }        @Test     public void testGetFileInfo(){         try {             ClientGlobal.init(conf_filename);            TrackerClient tracker = new TrackerClient();             TrackerServer trackerServer = tracker.getConnection();             StorageServer storageServer = null;            StorageClient storageClient = new StorageClient(trackerServer, storageServer);             FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             System.out.println(fi.getSourceIpAddr());             System.out.println(fi.getFileSize());             System.out.println(fi.getCreateTimestamp());             System.out.println(fi.getCrc32());         } catch (Exception e) {             e.printStackTrace();         }     }         @Test     public void testGetFileMate(){         try {             ClientGlobal.init(conf_filename);            TrackerClient tracker = new TrackerClient();             TrackerServer trackerServer = tracker.getConnection();             StorageServer storageServer = null;            StorageClient storageClient = new StorageClient(trackerServer,                     storageServer);             NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             for(NameValuePair nvp : nvps){                 System.out.println(nvp.getName() + ":" + nvp.getValue());             }         } catch (Exception e) {             e.printStackTrace();         }     }         @Test     public void testDelete(){         try {             ClientGlobal.init(conf_filename);            TrackerClient tracker = new TrackerClient();             TrackerServer trackerServer = tracker.getConnection();             StorageServer storageServer = null;            StorageClient storageClient = new StorageClient(trackerServer,                     storageServer);             int i = storageClient.delete_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             System.out.println( i==0 ? "删除成功" : "删除失败:"+i);         } catch (Exception e) {             e.printStackTrace();         }     }}

 

转载于:https://www.cnblogs.com/jiafuwei/p/5777622.html

你可能感兴趣的文章
一个oracle存储过程
查看>>
.NET MVC伪静态
查看>>
100 个最常用的 PHP 函数
查看>>
88. Merge Sorted Array java solutions
查看>>
347. Top K Frequent Elements java solutions
查看>>
面试总结
查看>>
EL表达式
查看>>
5:练习题
查看>>
【转帖】快学正则表达式
查看>>
bzoj 1070: [SCOI2007]修车 -- 费用流
查看>>
Tomcat设计模式
查看>>
BZOJ1041 HAOI2008圆上的整点(数论)
查看>>
GitHub详细教程
查看>>
分布式事务、两阶段提交协议、三阶提交协议
查看>>
差分约束
查看>>
UINavigationCountroller pop到指定界面
查看>>
NoSQL相关数据库
查看>>
安卓扁平化之路专题(四)Material Design
查看>>
案例:音乐列表
查看>>
npm
查看>>