Interesting things
因为元旦,公司放3天假。但是这3天里面也不敢松懈,继续撸FastDFS了。
What did you do today
-
我们查看fastdfs-client-java的源码,根据自己的需求封装FastDFSUtil,核心方法为upload()、download()、delete()、getFileInfo()、getFileMetaData()。代码没什么好说的,用到的设计模式是单例模式,是用静态内部类实现的单例。
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"); } } }
-
整个项目结构是这样子的。
- 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
![image.png](http://upload-images.jianshu.io/upload_images/4636177-6a5d45a6bc7534bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](http://upload-images.jianshu.io/upload_images/4636177-87c9f39aebacbe0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 测试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。
![image.png](http://upload-images.jianshu.io/upload_images/4636177-9ffa0afc8ac0cc9a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](http://upload-images.jianshu.io/upload_images/4636177-0d53c5d27105610c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 测试getFileInfo()方法
//gets "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file info
FastDFSUtil.getInstance().getFileInfo("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
* 获取文件信息成功!
![image.png](http://upload-images.jianshu.io/upload_images/4636177-81c16a6023bff133.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 测试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());
}
* 获取文件元信息成功!
![image.png](http://upload-images.jianshu.io/upload_images/4636177-d9d98112a917710b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 测试detele()方法
//detele "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg" file
FastDFSUtil.getInstance().delete("group1", "M00/00/00/wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg");
![image.png](http://upload-images.jianshu.io/upload_images/4636177-b7f52a33a23fb228.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 删除成功!我们进入192.168.12.33和192.168.12.44的cd /fastdfs/storage/data/M00/00/00/目录下面已经找不到wKgMLFpHPe-AOvAYAAd8hCbLY3Y881.jpg了。
![image.png](http://upload-images.jianshu.io/upload_images/4636177-9dd29e85c016cf0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](http://upload-images.jianshu.io/upload_images/4636177-1261167ca5dffc27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 最后附上完整测试用例。
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
> 写完了,就要陪女朋友吃螺蛳粉,逛万达广场,看电影咯。
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦