一、背景

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

二、使用步骤

  1.开发环境

    spring+springmvc+maven

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

 <dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>5.0.4</version>
</dependency>

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

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

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

package com.hafiz.fastdfs;

import java.io.FileNotFoundException;
import java.io.IOException; import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test; import com.taotao.common.utils.FastDFSClient; public class FastdfsTest { private static final String CONFIGLOCATION = "D:\\fastdfs_client.conf"; @Test
public void testUploadImg () {
try {
// 初始化全局配置。加载client配置文件
ClientGlobal.init(CONFIGLOCATION);
// 创建一个TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
// 创建一个TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
// 声明一个StorageServer对象并初始为null
StorageServer storageServer = null;
// 获得StorageClient对象
StorageClient storageClient = new StorageClient(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();
}
} @Test
public void fastDfsClientTest() {
try {
FastDFSClient client = new FastDFSClient(CONFIGLOCATION);
String imgUrl = client.uploadFile("D:\\Documents\\Downloads\\高圆圆1.jpg", "jpg", null);
System.out.println(imgUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}

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

package com.hafiz.common.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer; public class FastDFSClient { private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) {
String url = this.getClass().getResource("/").getPath();
url = url.substring(1);
conf = conf.replace("classpath:", url);
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
return storageClient.upload_file1(fileName, extName, metas);
}
public String uploadFile(String fileName, String extName) throws Exception {
return storageClient.upload_file1(fileName, extName, null);
} public String uploadFile(String fileName) throws Exception {
return storageClient.upload_file1(fileName, null, null);
}
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
return storageClient.upload_file1(fileContent, extName, metas);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return storageClient.upload_file1(fileContent, extName, null);
}
public String uploadFile(byte[] fileContent) throws Exception {
return storageClient.upload_file1(fileContent, null, null);
} }

三、总结

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

最新文章

  1. UI第十九节——UICollectionView
  2. 解决关键SSL安全问题和漏洞
  3. Code of Conduct
  4. 渗透测试报告收集、生成工具MagicTree
  5. iOS之UI--CAGradientLayer
  6. mysqldump 备份原理9
  7. IntelliJ IDEA: maven &amp; jetty 开发 java web
  8. OC基础4:类和方法
  9. appfog java jdbc mysql连接
  10. @postconstruct初始化的操作
  11. UNDO(二)
  12. springcloud-spring cloud config统一配置中心
  13. Delphi XE2 新增 System.Zip 单元, 可用一句话压缩整个文件夹了
  14. CString数组和CStringArray
  15. 云计算的三种服务模式:IaaS, PaaS, SaaS
  16. CSS选择器优先级(转)
  17. 【JAVA与C#比较】其它
  18. 【WP8】MultiBinding
  19. 如使用Typescript撸Vue(Vue2 + TS +TSX+CSS module)
  20. 【转】如何搭建IPv6测试你的APP

热门文章

  1. C++ —— 重载、覆盖和隐藏
  2. 开发.Net Script 模板-MyGeneration (翻译)
  3. 微信官方api &amp; 非官方api
  4. (转)Tomcat(java运行环境)安装及配置教程
  5. 20155326 第十周课下作业-IPC
  6. Android创建AVD模拟器
  7. hdu 5054
  8. ICCV2013、CVPR2013、ECCV2013目标检测相关论文
  9. 实验6 LCD接口
  10. 查询字段描述sql-postgresql