当前位置:网站首页>FastDfs的快速入门,三分钟带你上传下载文件到云服务器
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
2022-07-04 20:08:00 【掉头发的王富贵】
前提条件,linux安装fastdfs,如果没有安装的话请查看博主上一篇文章进行安装,否则无法进行下去。
https://blog.csdn.net/csdnerM/article/details/125179633
第一步,下载源文件
下载地址:https://codeload.github.com/happyfish100/fastdfs-client-java/zip/master
解压之后他是一个源文件,所以需要大家进行打包成一个jar包并且在自己的maven仓库去引用。
第二步,maven项目中引用
打包之后我们看到版本是1.29-SNAPSHOT,所以我们在maven中引入:
第三步,编写fastdfs.conf文件:

tracker_server=127.0.0.1:22122
此处填写自己服务器的ip和fastdfs的端口
第四步,编写工具类,实现上传功能
public static void upload() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
//加载配置文件,目的是为了获取所有的TrackerServer的地址信息
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
//创建Storage的客户端对象,需要利用这个对象来操作FastDFS,实现文件的上传下载和删除
sc = new StorageClient(ts, ss);
//上传文件到FastDFS
//参数 1 为需要上传的文件在本地磁盘的绝对路径
//参数 2 为需要上传的文件的扩展名
//参数 3 为需要上传的文件的属性文件通常为null不上传,这些文件的属性例如文件大小以及类型等信息通常需要记录到数据库中
//返回一个字符串数组,这个数组中的数据非常重要必须要妥善保管
//注意:这个数组中的第一个元素为文件所在的FastDFS的组名,第二个元素为文件在FastDFS中的远程文件名称
// 这两个数据通常我们是需要写入到数据库中的
String[] result = sc.upload_file("/Users/wangfugui/Downloads/20220529151321.png", "png", null);
for (String str : result) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
if (sc != null) {
try {
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

第五步,编写下载方法
public static void download() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
//加载配置文件,目的是为了获取所有的TrackerServer的地址信息
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
//创建Storage的客户端对象,需要利用这个对象来操作FastDFS,实现文件的上传下载和删除
sc = new StorageClient(ts, ss);
int downloadFile = sc.download_file("group1", "M00/00/00/rBJzqmKgR4OACNHEABeAcCmFn1g961.png", "/Users/wangfugui/Downloads/ss.png");
System.out.println(downloadFile);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
try {
if (sc != null) {
sc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

第六步,编写删除方法
public static void delete() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
//加载配置文件,目的是为了获取所有的TrackerServer的地址信息
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
//创建Storage的客户端对象,需要利用这个对象来操作FastDFS,实现文件的上传下载和删除
sc = new StorageClient(ts, ss);
int deleteFile = sc.delete_file("group1", "M00/00/00/rBJzqmKgR4OACNHEABeAcCmFn1g961.png");
System.out.println(deleteFile);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} finally {
try {
if (sc != null) {
sc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

第七步,编写main方法进行测试

上传之后返回一个数组,里面的东西长这样:
group1
M00/00/00/rBJzqmKgR4OACNHEABeAcCmFn1g961.png
我们去/00/00/文件夹里面看一下:
删除方法,传入组名称和文件路径,就会发现服务器里面的文件删除了,
下载方法传入组名称和文件路径和本地路径,就可以发现下载成功:

仓库地址:
边栏推荐
- BFC面试简述
- 宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
- 【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
- uniapp 富文本编辑器使用
- What should I do if my computer sharing printer refuses access
- Gobang go to work fishing tools can be LAN / man-machine
- colResizable.js自动调整表格宽度插件
- How does wincc7.5 SP1 find variables and their positions through cross indexing?
- What if the brightness of win11 is locked? Solution to win11 brightness locking
- Leetcode+ 81 - 85 monotone stack topic
猜你喜欢

看腾讯大老如何做接口自动化测试

字节测试工程师十年经验直击UI 自动化测试痛点

Qt五子棋人机对战画棋子之QPainter的使用误区总结

Managed service network: application architecture evolution in the cloud native Era

接口設計時的一些建議

伦敦银走势图分析的新方法

Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t

PS vertical English and digital text how to change direction (vertical display)

From automation to digital twins, what can Tupo do?

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
随机推荐
奏响青春的乐章
字节测试工程师十年经验直击UI 自动化测试痛点
How does wincc7.5 SP1 find variables and their positions through cross indexing?
GVM使用
Alibaba testers use UI automated testing to achieve element positioning
剑指 Offer II 80-100(持续更新)
uniapp 富文本编辑器使用
PS竖排英文和数字文字怎么改变方向(变竖直显示)
VIM asynchronous problem
《动手学深度学习》(三) -- 卷积神经网络 CNN
阿里云国际版CDN的优势
Idea configuration standard notes
[Shenbo introduction] VI How to contact your favorite doctoral tutor
Browser render page pass
nmap扫描
Common verification rules of form components -1 (continuously updating ~)
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system