当前位置:网站首页>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/文件夹里面看一下:
删除方法,传入组名称和文件路径,就会发现服务器里面的文件删除了,
下载方法传入组名称和文件路径和本地路径,就可以发现下载成功:
仓库地址:
边栏推荐
猜你喜欢
How does wincc7.5 SP1 find variables and their positions through cross indexing?
Leetcode+ 81 - 85 monotone stack topic
Foxit pdf editor v10.1.8 green version
Hands on deep learning (III) -- convolutional neural network CNN
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
分析伦敦银走势图的技巧
RFID仓储管理系统解决方案的优点
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
Jmeter 之压测入门
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
随机推荐
Hands on deep learning (III) -- convolutional neural network CNN
伦敦银走势图分析的新方法
接口设计时的一些建议
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
BFC interview Brief
go笔记(1)go语言介绍以及特点
go语言笔记(4)go常用管理命令
Jekins initialization password not found or not found
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Android原生数据库的基本使用和升级
go语言笔记(2)go一些简单运用
《动手学深度学习》(三) -- 卷积神经网络 CNN
Idea plug-in
记录线上bug解决list(未完待续7/4)
Hwinfo hardware detection tool v7.26 green version
What are the functional modules of RFID warehouse management system solution
BFC面试简述
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Sword finger offer II 80-100 (continuous update)
TweenMax表情按钮js特效