四、分布式文件系统fastDFS-Java Api
FastDFS是通过StorageClient来执行上传操作的
通过看源码我们知道,FastDFS有两个StorageClient工具类
StorageClient的上传方法upload_file(...)返回的是字符串数组String[],
如[group1,M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg]
StorageClient1的上传方法upload_file(...)返回的是字符串String,
如group1/M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg,也就是已经帮我们拼接好了
所以使用StorageClient1的上传方法更方便,不用我们自己拼接了。
原文: http://www.cnblogs.com/winner-0715/p/5516612.html
FastDFS是通过StorageClient来执行上传操作的
通过看源码我们知道,FastDFS有两个StorageClient工具类
StorageClient的上传方法upload_file(...)返回的是字符串数组String[],
如[group1,M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg]
StorageClient1的上传方法upload_file(...)返回的是字符串String,
如group1/M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg,也就是已经帮我们拼接好了
所以使用StorageClient1的上传方法更方便,不用我们自己拼接了。
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
//使用StorageClient1进行上传
private StorageClient1 storageClient1 = null;
public FastDFSClient(String conf) throws Exception {
//获取classpath路径下配置文件"fdfs_client.conf"的路径
//conf直接写相对于classpath的位置,不需要写classpath:
String configPath = this.getClass().getClassLoader().getResource(conf).getFile();
System.out.println(configPath);
ClientGlobal.init(configPath);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
}
/**
* MultipartFile picFile: file_buff = picFile.getBytes()
*/
public String uploadFile(byte[] file_buff, String file_ext_name) throws Exception {
String result = storageClient1.upload_file1(file_buff, file_ext_name, null);
return result;
}
public String uploadFile(String local_filename, String file_ext_name) throws Exception {
String result = storageClient1.upload_file1(local_filename, file_ext_name, null);
return result;
}
//test
public static void main(String[] args) throws Exception {
FastDFSClient client = new FastDFSClient("properties/fdfs_client.conf");
String result = client.uploadFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\aaa.jpg", "jpg");
System.out.println(result);
}
}
原文: http://www.cnblogs.com/winner-0715/p/5516612.html