packagecom.digi_zones.fdfs;importorg.apache.commons.io.FileUtils;importorg.apache.commons.io.IOUtil;importorg.csource.common.NameValuePair;import org.csource.fastdfs.*;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;/***
Description: FastDFS文件上传下载工具类
*
Copyright: Copyright (c) 2016
*
*@authoryangxin
*@version1.0
* @date 2016/10/19*/
public classFastDFSClient {private static final String CONFIG_FILENAME = "src/main/resources/fdfs/fdfs_client.conf";private static StorageClient1 storageClient1 = null;//初始化FastDFS Client
static{try{
ClientGlobal.init(CONFIG_FILENAME);
TrackerClient trackerClient= newTrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer=trackerClient.getConnection();if (trackerServer == null) {throw new IllegalStateException("getConnection return null");
}
StorageServer storageServer=trackerClient.getStoreStorage(trackerServer);if (storageServer == null) {throw new IllegalStateException("getStoreStorage return null");
}
storageClient1= newStorageClient1(trackerServer,storageServer);
}catch(Exception e) {
e.printStackTrace();
}
}/*** 上传文件
*@paramfile 文件对象
*@paramfileName 文件名
*@return
*/
public staticString uploadFile(File file, String fileName) {return uploadFile(file,fileName,null);
}/*** 上传文件
*@paramfile 文件对象
*@paramfileName 文件名
*@parammetaList 文件元数据
*@return
*/
public static String uploadFile(File file, String fileName, MapmetaList) {try{byte[] buff = IOUtil.toByteArray(newFileInputStream(file));
NameValuePair[] nameValuePairs= null;if (metaList != null) {
nameValuePairs= newNameValuePair[metaList.size()];int index = 0;for (Iterator> iterator =metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry =iterator.next();
String name=entry.getKey();
String value=entry.getValue();
nameValuePairs[index++] = newNameValuePair(name,value);
}
}returnstorageClient1.upload_file1(buff,FileUtils.getExtension(fileName),nameValuePairs);
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** 获取文件元数据
*@paramfileId 文件ID
*@return
*/
public static MapgetFileMetadata(String fileId) {try{
NameValuePair[] metaList=storageClient1.get_metadata1(fileId);if (metaList != null) {
HashMap map = new HashMap();for(NameValuePair metaItem : metaList) {
map.put(metaItem.getName(),metaItem.getValue());
}returnmap;
}
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** 删除文件
*@paramfileId 文件ID
*@return删除失败返回-1,否则返回0*/
public static intdeleteFile(String fileId) {try{returnstorageClient1.delete_file1(fileId);
}catch(Exception e) {
e.printStackTrace();
}return -1;
}/*** 下载文件
*@paramfileId 文件ID(上传文件成功后返回的ID)
*@paramoutFile 文件下载保存位置
*@return
*/
public static intdownloadFile(String fileId, File outFile) {
FileOutputStream fos= null;try{byte[] content =storageClient1.download_file1(fileId);
fos= newFileOutputStream(outFile);
IOUtil.copy(content,fos);return 0;
}catch(Exception e) {
e.printStackTrace();
}finally{if (fos != null) {try{
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}return -1;
}
}