前言
上篇博客我们简单了解了fastdfs,接下来我们就利用fastdfs来实现文件存储的功能
fastdfs的使用方法
1、把FastDFS提供的jar包添加到工程中
2、初始化全局配置。加载一个配置文件。
3、创建一个TrackerClient对象。
4、创建一个TrackerServer对象。
5、声明一个StorageServer对象,null。
6、获得StorageClient对象。
7、直接调用StorageClient对象方法上传文件即可。
导入jar包
由于中央仓库没有fastdfs-clinent的包,我们可以利用fastdfs-clinent的maven工程来生成jar包。首先我们将fastdfs工程导入到项目中,然后执行“Maven Install”,打包到本地仓库。然后在web的pom文件中添加对fastdfs的依赖,这样我们就可以在maven依赖项中看到fastdfs的jar包。
FastDFS-Clinent工程如下:
fastdfs maven工程导入到项目中,以及fastdfs依赖项如下:
创建配置文件
配置文件的作用就是加载Tracker Serve,然后创建TrackerClient对象,所以配置文件中的内容仅仅只是Tracker server的Ip和端口号。
测试方法
@Test
public void testUpload() throws Exception {
// 1、把FastDFS提供的jar包添加到工程中
// 2、初始化全局配置。加载一个配置文件。括号中为配置文件的全路径
ClientGlobal.init("D:\\workspaces-itcast\\JaveEE18\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
// 3、创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 4、创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 6、获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 7、直接调用StorageClient对象方法上传文件即可。第一个参数是文件的全路径,第二个参数是文件的扩展名,第三个参数是文件属性。
String[] strings = storageClient.upload_file("D:\\Documents\\Pictures\\images\\2f2eb938943d.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
}
执行方法
执行结果如下,回显信息的第一行是该图片被保存到哪个组了,由于我们现在只是用的单机FastDFS服务器,因此现在都是group1。第二行是存放的具体位置。
访问的时候我们需要将Tracker Serverde IP返回的组以及存放路径拼接起来访问才行。
使用fastdfs工具类存储文件
一个项目可能有多个地方需要上传文件,这是我们不可能去复制粘贴代码,合理可行的方法是封装,然后调用即可。接下来我们就将Fastdfs工具类放到common工程中,工具类代码如下:
package com.taotao.shared.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:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
使用工具类的测试方法
@Test
public void testFastDfsClient() throws Exception {
//实例化工具类,并传入Tracker server配置文件的路径。
FastDFSClient client = new FastDFSClient("D:\\workspaces-itcast\\JaveEE18\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
String uploadFile = client.uploadFile("D:\\Documents\\Pictures\\images\\200811281555127886.jpg", "jpg");
System.out.println(uploadFile);
}
结语
这只是简单的使用,而关于fastdfs和处理负载均衡的nginx服务器的原理,还需结合项目去实践,加深理解,加油!