当前位置:网站首页>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/文件夹里面看一下:
删除方法,传入组名称和文件路径,就会发现服务器里面的文件删除了,
下载方法传入组名称和文件路径和本地路径,就可以发现下载成功:

仓库地址:
边栏推荐
- colResizable.js自动调整表格宽度插件
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
- LeetCode 7. Integer inversion
- In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
- 阿里云国际版CDN的优势
- Summary of the mistakes in the use of qpainter in QT gobang man-machine game
- See how Tencent does interface automation testing
- TweenMax表情按钮js特效
- 阿里测试师用UI自动化测试实现元素定位
猜你喜欢

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

colResizable.js自动调整表格宽度插件

网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??

福昕PDF编辑器v10.1.8绿色版

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

TweenMax表情按钮js特效

From automation to digital twins, what can Tupo do?

Some suggestions for interface design

D3.js+Three.js数据可视化3d地球js特效
![[server data recovery] a case of RAID5 data recovery stored in a brand of server](/img/a2/e03dcb30276d115969fbd00f1830dc.png)
[server data recovery] a case of RAID5 data recovery stored in a brand of server
随机推荐
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
How to solve the problem that win11 cannot write the value to the registry key?
实战模拟│JWT 登录认证
冰河的海报封面
The concept and application of hash table
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
阿里云国际版CDN的优势
接口设计时的一些建议
word中插入图片后,图片上方有一空行,且删除后布局变乱
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
nmap扫描
go笔记(3)Go语言fmt包的用法
Some suggestions for interface design
Idea configuration standard notes
TweenMax表情按钮js特效
Actual combat simulation │ JWT login authentication
伦敦银走势图分析的新方法
VIM asynchronous problem
Implementation of redis distributed lock
mysql语句执行详解