前言
FastDFS是用c语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。
引入jar包
需要使用fastdfs_client_v1.20.jar,点击下载,并将此jar加入到工程中。
使用步骤
public void upload() throws Exception{
//创建一个配置文件内容为tracker服务器的地址
//使用全局对象来加载配置文件
ClientGlobal.init("全局配置文件的全路径,该配置文件内容为<tracker_server=FastDFS服务器ip:端口号>");
//创建一个trackerClient对象
TrackerClient trackerClient = new TrackerClient();
//通过trackerClient获得一个trackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
//创建一个strorageServer的引用,可以为null
StorageServer storageServer = null;
//创建一个storageClient,参数需要TrackerServer和StrorageServer
StorageClient storageClient = new StorageClient(trackerServer,storageServer);
//使用StorageClient上传文件
String[] strings = storageClient.upload_file("待上传图片全路径", "图片格式,如jpg,不需要加.", null);
for(String string : strings) {
System.out.println(string);
}
}
执行此方法后,控制台打印上传后图片的地址以及名称,如下:
文件索引信息包括
(1)组名:文件上传后所在的storage组名称,在文件上传成功后有storage服务器返回,需要客户端自行保存。
(2)虚拟磁盘路径:storage配置的虚拟路径,与磁盘选项store_path*对应。如果配置了store_path0则是M00,如果配置了store_path1则是M01,以此类推。
(3)数据两级目录:storage服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。
(4)文件名:与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器IP地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息。
上传成功后,访问http://FastDFS文件服务器ip/该全路径即可。
使用工具类
FastDFSClient.java
package cn.itcast.fastdfs.cliennt;
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);
}
}
客户端使用
public void fastDfsClient() throws Exception{
FastDFSClient fastDFSClient = new FastDFSClient("配置文件地址,可用classpath相对路径,工具类提供转换");
String string = fastDFSClient.uploadFile("待上传图片全路径");
System.out.println(string);
}
总结
工具类的使用能极大的方便我们的编程,工具类的思想也就是面向对象的特性之——封装。