fastdfs javaclient
1.pom
1 <dependency> 2 <groupId>net.oschina.zcx7878</groupId> 3 <artifactId>fastdfs-client-java</artifactId> 4 <version>1.27.0.0</version> 5 </dependency>
2.工具类
1 import org.csource.common.NameValuePair; 2 import org.csource.fastdfs.*; 3 import org.springframework.core.io.ClassPathResource; 4 import java.io.ByteArrayInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 public class FastDFSClient { 9 10 11 12 13 /**** 14 * 初始化Tracker的链接信息 15 * 实现文件上传 16 * 必须知道Tracker的链接IP和端口 17 */ 18 static { 19 try { 20 //1.读取文件 21 String filePath = new ClassPathResource("fdfs_client.conf").getPath();//类路径下的文件名字 22 //2.使用FastDFS客户端初始化加载数据 23 ClientGlobal.init(filePath); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 29 /**** 30 * 文件上传 31 */ 32 public static String[] upload(FastDFSFile file) throws Exception{ 33 //自定义扩展参数 34 NameValuePair[] meta_list = new NameValuePair[2]; 35 meta_list[0]=new NameValuePair("设备","HUAWEI Mate 90 Pro Max Plus 9G"); 36 meta_list[1]=new NameValuePair("地点","北京"); 37 38 //1、请求TrackerServer 39 StorageClient storageClient = getStorageClient(); 40 41 /*** 42 * 4、根据存储的Storage信息实现文件上传 43 * 1:文件的字节数组 44 * 2:文件的扩展名(后缀,jpg,mp4) 45 * 3:自定义扩展参数 46 */ 47 String[] backlist = storageClient.upload_file(file.getContent(), file.getExt(), meta_list); 48 49 50 return backlist; 51 52 53 } 54 55 56 /*** 57 * 获取文件信息 58 */ 59 public static FileInfo getFile(String groupName, String remoteFileName) throws Exception{ 60 //1、请求TrackerServer 61 StorageClient storageClient = getStorageClient(); 62 63 /*** 64 * 4、获取文件信息 65 * 1:文件所属的组名 66 * 2:文件的存储路径信息 67 */ 68 return storageClient.get_file_info(groupName,remoteFileName); 69 } 70 71 72 /*** 73 * 文件下载-Storage 74 * @throws Exception 75 */ 76 public static InputStream downFile(String groupName, String remoteFileName)throws Exception{ 77 StorageClient storageClient = getStorageClient(); 78 79 /*** 80 * 4、文件下载 81 * 1:文件所属的组名 82 * 2:文件的存储路径信息 83 **/ 84 byte[] bytes = storageClient.download_file(groupName, remoteFileName); 85 86 //将字节数组转换成InputStream 87 ByteArrayInputStream is = new ByteArrayInputStream(bytes); 88 return is; 89 } 90 91 /*** 92 * 抽取获取StorageClient 93 * @return 94 * @throws IOException 95 */ 96 public static StorageClient getStorageClient() throws IOException { 97 //1、请求TrackerServer 98 TrackerClient client = new TrackerClient(); 99 100 //2、请求TrackerServer后返回的数据包含Storage的信息 101 TrackerServer trackerServer = client.getConnection(); 102 103 //3、取出Storage信息,并且存储起来(storageClient) 104 return new StorageClient(trackerServer,null); 105 } 106 107 108 /*** 109 * 文件删除 110 * @throws Exception 111 */ 112 public static int delete(String groupName, String remoteFileName) throws Exception{ 113 //1、请求TrackerServer 114 StorageClient storageClient = getStorageClient(); 115 116 //4、文件删除 117 int count = storageClient.delete_file(groupName, remoteFileName); 118 return count; 119 } 120 121 122 123 /*** 124 * 文件删除 125 * @throws Exception 126 */ 127 public static int delete(String path) throws Exception{ 128 String[] split = path.split("/"); 129 String groupName=""; 130 String remoteFileName=""; 131 for (int i=0;i<split.length;i++){ 132 if (split[i].startsWith("group")){ 133 groupName=split[i]; 134 String[] split1 = path.split(split[i]); 135 remoteFileName = split1[1].substring(1); 136 break; 137 } 138 } 139 140 //1、请求TrackerServer 141 StorageClient storageClient = getStorageClient(); 142 143 //4、文件删除 144 int count = storageClient.delete_file(groupName, remoteFileName); 145 return count; 146 } 147 148 /*** 149 * 获取Storage组信息 150 * @throws Exception 151 */ 152 public static StorageServer getStorages(String groupName) throws Exception{ 153 //1、请求TrackerServer 154 TrackerClient client = new TrackerClient(); 155 156 //2、请求TrackerServer后返回的数据包含Storage的信息 157 TrackerServer trackerServer = client.getConnection(); 158 159 //3、获取指定组的信息 160 return client.getStoreStorage(trackerServer,groupName); 161 } 162 163 164 /*** 165 * 获取相关的Storage服务信息 166 * @throws Exception 167 */ 168 public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception{ 169 //1、请求TrackerServer 170 TrackerClient client = new TrackerClient(); 171 172 //2、请求TrackerServer后返回的数据包含Storage的信息 173 TrackerServer trackerServer = client.getConnection(); 174 175 //3、获取Storage信息 176 return client.getFetchStorages(trackerServer,groupName,remoteFileName); 177 } 178 179 /**** 180 * 获取Tracker的信息 181 * @throws Exception 182 */ 183 public static String getTrackerUrl() throws Exception{ 184 //1、请求TrackerServer 185 TrackerClient client = new TrackerClient(); 186 187 //2、请求TrackerServer后返回的数据包含Storage的信息 188 TrackerServer trackerServer = client.getConnection(); 189 190 //获取Tracker相关信息 TCP 11211/Http 8080 191 String url="Http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port(); 192 return url; 193 } 194 195 public static void main(String[] args) throws Exception{ 196 //获取文件信息测试 197 //FileInfo fileInfo = getFile("group1","M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg"); 198 //System.out.println("IP:"+fileInfo.getSourceIpAddr()); 199 200 //文件下载 201 //InputStream is = downFile("group1", "M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg"); 202 //创建字节输出流 203 //FileOutputStream os = new FileOutputStream("D:/123.jpg"); 204 //byte[] buffer = new byte[1024]; 205 //while (is.read(buffer)!=-1){ 206 // os.write(buffer); 207 //} 208 //关闭资源 209 //is.close(); 210 //os.close(); 211 212 //删除文件 213 //delete("group1", "M00/00/00/wKjThF3668eASWAHAAnAAJuzIB4619.jpg"); 214 215 //获取Storage信息 216 //StorageServer group1 = getStorages("group1"); 217 //System.out.println(group1.getStorePathIndex()); 218 219 //获取指定文件所在的Storage组的信息 220 //ServerInfo[] serverInfo = getServerInfo("group1", "M00/00/00/wKjThF369TOAA2BUAAnAAJuzIB4033.jpg"); 221 //for (ServerInfo info : serverInfo) { 222 // System.out.println(info.getIpAddr() + ":" + info.getPort()); 223 //} 224 225 String trackerUrl = getTrackerUrl(); 226 System.out.println(trackerUrl); 227 } 228 229 230 }
3.实体类
1 public class FastDFSFile { 2 3 //文件名字 4 private String name; 5 //文件内容 6 private byte[] content; 7 //文件扩展名 8 private String ext; 9 //文件MD5摘要值 10 private String md5; 11 //文件创建作者 12 private String author; 13 14 public FastDFSFile(String name, byte[] content, String ext, String md5, String author) { 15 this.name = name; 16 this.content = content; 17 this.ext = ext; 18 this.md5 = md5; 19 this.author = author; 20 } 21 22 public FastDFSFile(String name, byte[] content, String ext) { 23 this.name = name; 24 this.content = content; 25 this.ext = ext; 26 } 27 28 public FastDFSFile() { 29 } 30 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public byte[] getContent() { 41 return content; 42 } 43 44 public void setContent(byte[] content) { 45 this.content = content; 46 } 47 48 public String getExt() { 49 return ext; 50 } 51 52 public void setExt(String ext) { 53 this.ext = ext; 54 } 55 56 public String getMd5() { 57 return md5; 58 } 59 60 public void setMd5(String md5) { 61 this.md5 = md5; 62 } 63 64 public String getAuthor() { 65 return author; 66 } 67 68 public void setAuthor(String author) { 69 this.author = author; 70 } 71 }
4.配置文件Classpath目录下命名为:fdfs_client.conf
charset=UTF-8
http.tracker_http_port=8888
tracker_server=ip:22122
本文作者:zydjjcpdszylddpll
本文链接:https://www.cnblogs.com/jyfs/p/14143621.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步