文章插图
- 浏览器访问

文章插图
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);}}}}} 
文章插图

文章插图
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 技术整理】
推荐阅读
- 如何批量修改文件名
- 苹果|罗永浩谈苹果新系统发布:有点放飞自我 一口气抄了锤子四五个功能
- iOS|苹果iOS 16系统支持横向人脸解锁:老外感到很惊喜
- 苹果|教你第一时间升级更新!iOS 16 Beta描述文件下载
- Windows操作系统|Win11年度版本22H2特性曝光:回滚系统更方便
- 苹果|苹果发布WatchOS 9系统:4款炫酷表盘、吃药能提醒
- 苹果|苹果发布MacOS 13系统:3A游戏大作正式杀入Mac
- 苹果|今晚发布!iOS 16 Beta描述文件提前偷跑:图标变样了
- 人力资源档案管理系统查询 个人档案查询系统
- 微软|Edge成新一代传文件“神器”:新功能几乎可传所有内容
