四、分布式文件系统fastDFS-Java Api

四、分布式文件系统fastDFS-Java Api

FastDFS是通过StorageClient来执行上传操作的
通过看源码我们知道,FastDFS有两个StorageClient工具类

StorageClient的上传方法upload_file(...)返回的是字符串数组String[],
如[group1,M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg]

StorageClient1的上传方法upload_file(...)返回的是字符串String,
如group1/M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg,也就是已经帮我们拼接好了

所以使用StorageClient1的上传方法更方便,不用我们自己拼接了。

public class FastDFSClient {
    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    //使用StorageClient1进行上传
    private StorageClient1 storageClient1 = null;

    public FastDFSClient(String conf) throws Exception {
        //获取classpath路径下配置文件"fdfs_client.conf"的路径
        //conf直接写相对于classpath的位置,不需要写classpath:
        String configPath = this.getClass().getClassLoader().getResource(conf).getFile();
        System.out.println(configPath);
        ClientGlobal.init(configPath);

        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = trackerClient.getStoreStorage(trackerServer);
        storageClient1 = new StorageClient1(trackerServer, storageServer);
    }

   /**
      * MultipartFile picFile: file_buff = picFile.getBytes()
   */
    public String uploadFile(byte[] file_buff, String file_ext_name) throws Exception {
        String result = storageClient1.upload_file1(file_buff, file_ext_name, null);
        return result;
    }

    public String uploadFile(String local_filename, String file_ext_name) throws Exception {
        String result = storageClient1.upload_file1(local_filename, file_ext_name, null);
        return result;
    }

    //test
    public static void main(String[] args) throws Exception {
        FastDFSClient client = new FastDFSClient("properties/fdfs_client.conf");
        String result = client.uploadFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\aaa.jpg", "jpg");
        System.out.println(result);
    }

}

原文: http://www.cnblogs.com/winner-0715/p/5516612.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值