package com.bjpowernode.p2p.admin.fastdfs;
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;
/**
* fastdfs 文件上传
*
* @author ldd
*
*/
public class FastdfsClient {
/**
* 文件上传
*
* @param bytes 文件字节
* @param fileExtend 文件尾名 不需要.
* @return
*/
public static String[] uploadFile (byte[] bytes, String fileExtend) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
//利用fastdfs客户端,实现文件上传到fastdfs服务器上
try {
//代码是模板式的
//1、加载配置文件
ClientGlobal.init("fastdfs_client.conf");
//2、创建一个tracker的客户端
TrackerClient trackerClient = new TrackerClient();
//3、通过trackerClient获取一个连接,连接到Tracker,得到一个TrackerServer
trackerServer = trackerClient.getConnection();
//4、通过trackerClient获取一个存储节点的StorageServer
storageServer = trackerClient.getStoreStorage(trackerServer);
//5、通过trackerServer和storageServer构造一个Storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
return storageClient.upload_file(bytes, fileExtend, null);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
try {
//关闭服务,释放资源
if (null != storageServer) {
storageServer.close();
}
if (null != trackerServer) {
trackerServer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 文件上传
*
* @param bytes 本地文件名
* @param fileExtend 文件末尾名 不要.
* @return
*/
public static String[] uploadFile (String localFile, String fileExtend) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
//利用fastdfs客户端,实现文件上传到fastdfs服务器上
try {
//代码是模板式的
//1、加载配置文件
ClientGlobal.init("fastdfs_client.conf");
//2、创建一个tracker的客户端
TrackerClient trackerClient = new TrackerClient();
//3、通过trackerClient获取一个连接,连接到Tracker,得到一个TrackerServer
trackerServer = trackerClient.getConnection();
//4、通过trackerClient获取一个存储节点的StorageServer
storageServer = trackerClient.getStoreStorage(trackerServer);
//5、通过trackerServer和storageServer构造一个Storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
return storageClient.upload_file(localFile, fileExtend, null);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
try {
//关闭服务,释放资源
if (null != storageServer) {
storageServer.close();
}
if (null != trackerServer) {
trackerServer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 删除图片
*
* @param group
* @param filePath
* @return
*/
public static int deleteFile (String group, String filePath) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
//利用fastdfs客户端,实现文件上传到fastdfs服务器上
try {
//代码是模板式的
//1、加载配置文件
ClientGlobal.init("fastdfs_client.conf");
//2、创建一个tracker的客户端
TrackerClient trackerClient = new TrackerClient();
//3、通过trackerClient获取一个连接,连接到Tracker,得到一个TrackerServer
trackerServer = trackerClient.getConnection();
//4、通过trackerClient获取一个存储节点的StorageServer
storageServer = trackerClient.getStoreStorage(trackerServer);
//5、通过trackerServer和storageServer构造一个Storage客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//fastdfs删除文件
return storageClient.delete_file(group, filePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
try {
//关闭服务,释放资源
if (null != storageServer) {
storageServer.close();
}
if (null != trackerServer) {
trackerServer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return -1;
}
}