Java 客户端操作 FastDFS 实现文件上传下载替换删除 (2)

  

// 文件上传 @Test public void testUploadFile() { String[] fileids = FastDFSClient.uploadFile(new File("D:/china.jpg"), "china.jpg"); for (String fileid : fileids) { System.out.println("fileid = " + fileid); } }

  返回值:

fileid = group1 fileid = M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg

Java 客户端操作 FastDFS 实现文件上传下载替换删除

  

文件详情

  

// 查看文件详情 @Test public void testGetFileInfo() { FileInfo fileInfo = FastDFSClient.getFileInfo("group1", "M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg"); System.out.println("fileInfo = " + fileInfo); }

  返回值:

fileInfo = fetch_from_server = false, file_type = 1, source_ip_addr = 192.168.10.102, file_size = 57704, create_timestamp = 2020-09-28 08:44:08, crc32 = 645874781

  

文件元数据

  

// 获取文件数据 @Test public void testGetMetaData() { NameValuePair[] metaDatas = FastDFSClient.getMetaData("group1", "M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg"); for (NameValuePair metaData : metaDatas) { System.out.println(metaData.getName() + "---" + metaData.getValue()); } }

  返回值:

file_length---57704 file_name---china.jpg

  

文件下载

  

// 文件下载 @Test public void testDownloadFile() { InputStream is = FastDFSClient.downloadFile("group1", "M00/00/00/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg"); try (FileOutputStream fos = new FileOutputStream("D:/wKgKZl9xMdiAcOLdAADhaCZ_RF0096.jpg")) { int len = 0; byte[] bytes = new byte[1024]; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

  

文件删除

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssxdd.html