java 调用 fastdfs_使用java调用fastDFS客户端进行静态资源文件上传

一、背景

上篇博客我介绍了FastDFS的概念、原理以及安装步骤,这篇文章我们来聊一聊如何在java中使用FastDFSClient进行静态资源的上传。

二、使用步骤

1.开发环境

spring+springmvc+maven

2.首先在maven的pom.xml中引入依赖fastdfs-client的依赖

1

2 org.csource

3 fastdfs-client-java

4 5.0.4

5

3.接着我们来指定一个fastdfs-client.conf配置文件,里面内容如下:

tracker_server=host:port(这里指trackerServer服务器的ip和端口)

4.然后写一个单元测试类来测试服务

packagecom.hafiz.fastdfs;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.csource.common.MyException;importorg.csource.fastdfs.ClientGlobal;importorg.csource.fastdfs.StorageClient;importorg.csource.fastdfs.StorageServer;importorg.csource.fastdfs.TrackerClient;importorg.csource.fastdfs.TrackerServer;importorg.junit.Test;importcom.taotao.common.utils.FastDFSClient;public classFastdfsTest {private static final String CONFIGLOCATION = "D:\\fastdfs_client.conf";

@Testpublic voidtestUploadImg () {try{//初始化全局配置。加载client配置文件

ClientGlobal.init(CONFIGLOCATION);//创建一个TrackerClient对象

TrackerClient trackerClient = newTrackerClient();//创建一个TrackerServer对象

TrackerServer trackerServer =trackerClient.getConnection();//声明一个StorageServer对象并初始为null

StorageServer storageServer = null;//获得StorageClient对象

StorageClient storageClient = newStorageClient(trackerServer, storageServer);//直接调用StorageClient对象方法上传文件即可

String[] result = storageClient.upload_file("D:\\Documents\\Downloads\\高圆圆2.jpg", "jpg", null);for(String item : result) {

System.out.println(item);

}

trackerServer.close();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}catch(MyException e) {

e.printStackTrace();

}

}

@Testpublic voidfastDfsClientTest() {try{

FastDFSClient client= newFastDFSClient(CONFIGLOCATION);

String imgUrl= client.uploadFile("D:\\Documents\\Downloads\\高圆圆1.jpg", "jpg", null);

System.out.println(imgUrl);

}catch(Exception e) {

e.printStackTrace();

}

}

}

5.为了以后在项目中使用方便,我们不能每次都写这么一大串东西,所以我们来对该客户端进行以下封装:

packagecom.hafiz.common.utils;importorg.csource.common.NameValuePair;importorg.csource.fastdfs.ClientGlobal;importorg.csource.fastdfs.StorageClient1;importorg.csource.fastdfs.StorageServer;importorg.csource.fastdfs.TrackerClient;importorg.csource.fastdfs.TrackerServer;public classFastDFSClient {private TrackerClient trackerClient = null;private TrackerServer trackerServer = null;private StorageServer storageServer = null;private StorageClient1 storageClient = null;public FastDFSClient(String conf) throwsException {if (conf.contains("classpath:")) {

String url= this.getClass().getResource("/").getPath();

url= url.substring(1);

conf= conf.replace("classpath:", url);

}

ClientGlobal.init(conf);

trackerClient= newTrackerClient();

trackerServer=trackerClient.getConnection();

storageServer= null;

storageClient= newStorageClient1(trackerServer, storageServer);

}public String uploadFile(String fileName, String extName, NameValuePair[] metas) throwsException {returnstorageClient.upload_file1(fileName, extName, metas);

}public String uploadFile(String fileName, String extName) throwsException {return storageClient.upload_file1(fileName, extName, null);

}public String uploadFile(String fileName) throwsException {return storageClient.upload_file1(fileName, null, null);

}public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throwsException {returnstorageClient.upload_file1(fileContent, extName, metas);

}public String uploadFile(byte[] fileContent, String extName) throwsException {return storageClient.upload_file1(fileContent, extName, null);

}public String uploadFile(byte[] fileContent) throwsException {return storageClient.upload_file1(fileContent, null, null);

}

}

三、总结

通过以上的步骤,我们就完成在java中使用fastdfs客户端进行静态资源上传的功能,这里面我们得到一个最重要的思想就是:DRY(Don't Repeat Yourself!),要有封装的思想。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值