Interesting things
因為元旦输涕,公司放3天假涂乌。但是這3天里面也不敢松懈,繼續(xù)擼FastDFS了捎泻。
What did you do today
- 我們查看fastdfs-client-java的源碼侦鹏,根據(jù)自己的需求封裝FastDFSUtil诡曙,核心方法為upload()、download()略水、delete()价卤、getFileInfo()、getFileMetaData()聚请。代碼沒什么好說的荠雕,用到的設(shè)計模式是單例模式,是用靜態(tài)內(nèi)部類實現(xiàn)的單例驶赏。
public class FastDFSUtil {
private static String classPath = FastDFSUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
private static String FASTDFS_CLIENT_CONF = classPath + "fdfs_client.conf";
private static TrackerClient trackerClient = null;
private static TrackerServer trackerServer = null;
private static StorageClient storageClient = null;
private static StorageServer storageServer = null;
private FastDFSUtil () {
FastDFSUtil.init();
}
private static void init() {
try {
ClientGlobal.init(FASTDFS_CLIENT_CONF);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
throw new RuntimeException("init exception");
}
}
public static FastDFSUtil getInstance() {
return FastDFSUtilHolder.INSTANCE;
}
private static class FastDFSUtilHolder {
private static final FastDFSUtil INSTANCE = new FastDFSUtil();
}
public static String upload(String maydayImg, String fileExtName, NameValuePair[] metaDataList) {
try {
String fileIds[] = storageClient.upload_file(maydayImg, fileExtName, metaDataList);
System.out.println(fileIds.length);
System.out.println("group:" + fileIds[0]);
System.out.println("path:" + fileIds[1]);
return fileIds[0] + fileIds[1];
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("upload exception");
}
}
public static void download(String group, String file, String storePath) {
try {
byte[] data = storageClient.download_file(group, file);
IOUtils.write(data, new FileOutputStream(new File(storePath
+ "/" + file.substring(file.lastIndexOf("/") + 1))));
System.out.println("download success");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("download exception");
}
}
public static FileInfo getFileInfo(String group, String file) {
try {
FileInfo fileInfo = storageClient.get_file_info(group, file);
System.out.println("source ip=" + fileInfo.getSourceIpAddr());
System.out.println("file sizes=" + fileInfo.getFileSize());
System.out.println("timestamp=" + fileInfo.getCreateTimestamp());
System.out.println("crc32=" + fileInfo.getCrc32());
return fileInfo;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("gets file info exception");
}
}
public static NameValuePair[] getFileMataData(String group, String file) {
try {
NameValuePair[] metas = storageClient.get_metadata(group, file);
return metas;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("gets file metadata exception");
}
}
public static void delete(String group, String file) {
try {
storageClient.delete_file(group, file);
System.out.println("detele file success");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("deletes file exception");
}
}
}
-
整個項目結(jié)構(gòu)是這樣子的。
resources目錄下既鞠,還需要添加fdfs_client.conf煤傍。具體如下:
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8000
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.12.11:22122
tracker_server = 192.168.12.22:22122
- 測試upload()方法,我們上傳resources/upload/目錄下的mayday_life.png嘱蛋。
public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";
NameValuePair[] metaDataList = new NameValuePair[] {
new NameValuePair("bound_name", "mayday"),
new NameValuePair("masterpiece", "juejiang")
};
//tests upload mayday_life.jpg
FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);
-
上傳成功蚯姆!我們可以去訪問http://192.168.12.111/fastdfs/group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
測試download()方法
public static String BASE_DOWNLOAD_PATH =
"D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";
//download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
, BASE_DOWNLOAD_PATH);
-
下載成功!我們可以看到download文件夾下多了wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg洒敏。
- 測試getFileInfo()方法
//gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
-
獲取文件信息成功龄恋!
測試getFileMetaData()方法
//gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
for (NameValuePair nvp : metaDataList) {
System.out.println(nvp.getName() + ":" + nvp.getValue());
}
-
獲取文件元信息成功!
測試detele()方法
//detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
-
刪除成功凶伙!我們進(jìn)入192.168.12.33和192.168.12.44的cd /fastdfs/storage/data/M00/00/00/目錄下面已經(jīng)找不到wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg了郭毕。
最后附上完整測試用例。
public class TestFastDFS {
public static String BASE_UPLOAD_PATH = TestFastDFS.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
public static String MAYDAY_IMG = BASE_UPLOAD_PATH + "upload/mayday_life.jpg";
public static String BASE_DOWNLOAD_PATH =
"D:/myeclipse_workspace/fastdfs_demo/src/main/resources/download";
public static void main(String[] args) {
NameValuePair[] metaDataList = new NameValuePair[] {
new NameValuePair("bound_name", "mayday"),
new NameValuePair("masterpiece", "juejiang")
};
//tests upload mayday_life.jpg
//FastDFSUtil.getInstance().upload(MAYDAY_IMG, "jpg", metaDataList);
//upload success
//group:group1
//path:M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
//download group1/M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg
//FastDFSUtil.getInstance().download("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg"
//, BASE_DOWNLOAD_PATH);
//gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
//FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
//gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file metadata
//NameValuePair[] metaLists = FastDFSUtil.getInstance().getFileMataData("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
//for (NameValuePair nvp : metaDataList) {
//System.out.println(nvp.getName() + ":" + nvp.getValue());
//}
//detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
}
}
Summary
寫完了函荣,就要陪女朋友吃螺螄粉显押,逛萬達(dá)廣場,看電影咯傻挂。