1.fdfs_client.conf
tracker_server = 192.168.117.100:22122
2.FDFSTest.java
package com.siyuan.fastdfs; import java.io.File; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; public class FDFSTest { private static String CONF_PATH = null; static { String classPath; try { classPath = new File(FDFSTest.class.getResource("/").getFile()).getCanonicalPath(); CONF_PATH = classPath + File.separator + "fdfs_client.conf"; ClientGlobal.init(CONF_PATH); } catch (Exception e) { System.out.println("fail to init fastdfs configuration " + e); e.printStackTrace(); } } public static void main(String[] args) throws Exception { StorageClient storageClient = new StorageClient(); // 文件上传 NameValuePair[] meta_list = new NameValuePair[1]; meta_list[0] = new NameValuePair("originalName", "fdfs_client.conf"); String fileIds[] = storageClient.upload_file(CONF_PATH, null, meta_list); System.out.println(fileIds[0]); System.out.println(fileIds[1]); // 文件删除 storageClient.delete_file(fileIds[0], fileIds[1]); } }
建议:
1)fdfs_client.conf中配置多个tracker_server
2)使用无参构造构建StorageClient
原因:
每次操作都重新获取可用的TrackerServer并从中获取可用的StorageServer,使用实现HA
缺点:
每次请求都浪费几次请求