因为元旦,公司放3天假。但是这3天里面也不敢松懈,继续撸FastDFS了。
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");
}
}
}
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
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);
//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");
删除成功!我们进入192.168.12.33和192.168.12.44的cd /fastdfs/storage/data/M00/00/00/目录下面已经找不到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");
}
}
写完了,就要陪女朋友吃螺蛳粉,逛万达广场,看电影咯。