分布式文件系统FastDFS 技术整理( 七 )


文章插图
 

  • 浏览器访问

分布式文件系统FastDFS 技术整理

文章插图
 
 
2.4.2、文件下载
  • 把前面的文件上传代码改一下即可,换成另一个API而已
package com.zixieqing; import org.csource.common.MyException;import org.csource.fastdfs.*; import java.io.IOException; /** * @author : ZiXieQing * @version : V1.0.0 * @className : DownloadFile * @description : 该类功能 fastDFS文件下载 * @packageName : com.zixieqing */ public class DownloadFile {public static void main(String[] args) {TrackerServer trackerServer = null;StorageServer storageServer = null;try {// 1、初始化配置文件ClientGlobal.init("fastdfs.conf");// 2、获取tracker客户端TrackerClient trackerClient = new TrackerClient();// 3、获取trackerServertrackerServer = trackerClient.getConnection();// 4、获取storageServerstorageServer = trackerClient.getStoreStorage(trackerServer);// 5、创建storage客户端StorageClient storageClient = new StorageClient(trackerServer, storageServer);// 6、下载文件/*这里需要知道两个APIbyte[] download_file(String group_name, String remote_filename)这个API常用于web操作int download_file(String group_name, String remote_filename, String local_filename)这个API是把文件下载到本地磁盘中这个API的返回值结果很重要*/String group = "group1";String remoteFileName = "M00/00/00/CgAAEGKYRg-AAIrWAAD8cA4U6dY771.jpg";// 存入本地磁盘路径+存入磁盘的文件名String localFileName = "d:/靓妹.jpg";// 只有返回值是0才表示下载成功,否则只要是其他数字都是下载失败( 其他数字有可能是组名错了,远程文件名错了........int result = storageClient.download_file(group, remoteFileName, localFileName);// 7、验证System.out.println("result = " + result);} catch (IOException e) {throw new RuntimeException(e);} catch (MyException e) {throw new RuntimeException(e);} finally {// 8、释放资源if (storageServer != null) {try {storageServer.close();} catch (IOException e) {throw new RuntimeException(e);}}if (trackerServer != null) {try {trackerServer.close();} catch (IOException e) {throw new RuntimeException(e);}}}}}
分布式文件系统FastDFS 技术整理

文章插图
 

分布式文件系统FastDFS 技术整理

文章插图
 
 
2.4.3、文件删除package com.zixieqing; import org.csource.common.MyException;import org.csource.fastdfs.*; import java.io.IOException; /** * @author : ZiXieQing * @version : V1.0.0 * @className : DeleteFile * @description : 该类功能 FastDFS删除文件 * @packageName : com.zixieqing */ public class DeleteFile {public static void main(String[] args) {TrackerServer trackerServer = null;StorageServer storageServer = null;try {// 1、初始化配置文件ClientGlobal.init("fastdfs.conf");// 2、获取tracker客户端TrackerClient trackerClient = new TrackerClient();// 3、获取trackerServertrackerServer = trackerClient.getConnection();// 4、获取storageServerstorageServer = trackerClient.getStoreStorage(trackerServer);// 5、获取storage客户端StorageClient storageClient = new StorageClient(trackerServer, storageServer);// 6、执行文件删除/*int delete_file(String group_name, String remote_filename)参数1 group_name、组名参数2 remote_filename、远程文件名*/// 一样的,返回值是0就表示成功,其他都是删除失败int result = storageClient.delete_file("group", "M00/00/00/CgAAEGKYRg-AAIrWAAD8cA4U6dY771.jpg");// 7、验证一下System.out.println("result = " + result);} catch (IOException e) {throw new RuntimeException(e);} catch (MyException e) {throw new RuntimeException(e);}finally {// 8、释放资源if (storageServer != null) {try {storageServer.close();} catch (IOException e) {throw new RuntimeException(e);}}if (trackerServer != null) {try {trackerServer.close();} catch (IOException e) {throw new RuntimeException(e);}}}}}  
文章来自
https://www.cnblogs.com/xiegongzi/p/16330724.html

【分布式文件系统FastDFS 技术整理】


推荐阅读