当前位置:网站首页>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/文件夹里面看一下:
删除方法,传入组名称和文件路径,就会发现服务器里面的文件删除了,
下载方法传入组名称和文件路径和本地路径,就可以发现下载成功:
仓库地址:
边栏推荐
- What are the functional modules of RFID warehouse management system solution
- 接口設計時的一些建議
- BFC面试简述
- JS卡牌样式倒计时天数
- tcp为啥是三次握手和四次挥手
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- Leetcode+ 81 - 85 monotone stack topic
- 黄金k线图中的三角形有几种?
- Vue cleans up the keepalive cache scheme in a timely manner
- go笔记(3)Go语言fmt包的用法
猜你喜欢
Common verification rules of form components -1 (continuously updating ~)
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
Idea configuration standard notes
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 does the computer save web pages to the desktop for use
阿里测试师用UI自动化测试实现元素定位
idea恢复默认快捷键
Automatic generation of interface automatic test cases by actual operation
Sword finger offer II 80-100 (continuous update)
托管式服务网络:云原生时代的应用体系架构进化
随机推荐
go语言笔记(2)go一些简单运用
工厂从自动化到数字孪生,图扑能干什么?
Function analysis and source code of hash guessing game system development
[server data recovery] a case of RAID5 data recovery stored in a brand of server
LeetCode 8. 字符串转换整数 (atoi)
接口设计时的一些建议
TweenMax表情按钮js特效
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
【微服务|SCG】Predicate的使用
What if the brightness of win11 is locked? Solution to win11 brightness locking
Automatic generation of interface automatic test cases by actual operation
阿里测试师用UI自动化测试实现元素定位
记录线上bug解决list(未完待续7/4)
js 3D爆炸碎片图片切换js特效
go defer的使用说明
基于OpenCV haarcascades的对象检测
LeetCode+ 81 - 85 单调栈专题
acwing 3302. 表达式求值
LeetCode 8. String conversion integer (ATOI)
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】