当前位置:网站首页>A quick start to fastdfs takes you three minutes to upload and download files to the ECS
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
2022-07-04 21:16:00 【Wang Fugui who lost his hair】
Prerequisite ,linux install fastdfs, If it is not installed, please check the previous blog article to install , Otherwise it can't go on .
https://blog.csdn.net/csdnerM/article/details/125179633
First step , Download the source file
Download address :https://codeload.github.com/happyfish100/fastdfs-client-java/zip/master
After decompression, it is a source file , So we need to pack it into one jar Pack and in your own maven Warehouse dereference .
The second step ,maven Quote in the project
After packaging, we see that the version is 1.29-SNAPSHOT, So we are maven Introduction in :
The third step , To write fastdfs.conf file :

tracker_server=127.0.0.1:22122
Fill in your server's ip and fastdfs The port of
Step four , Write a tool class , Realize the upload function
public static void upload() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
// Load profile , The purpose is to get all TrackerServer Address information for
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
// establish Storage Client object of , You need to use this object to operate FastDFS, Upload, download and delete files
sc = new StorageClient(ts, ss);
// Upload files to FastDFS
// Parameters 1 The absolute path of the file to be uploaded on the local disk
// Parameters 2 Is the extension of the file to be uploaded
// Parameters 3 The attribute file for the file to be uploaded is usually null Do not upload , The attributes of these files, such as file size and type, usually need to be recorded in the database
// Returns an array of strings , The data in this array is very important and must be kept properly
// Be careful : The first element in this array is where the file is located FastDFS Group name , The second element is the file in FastDFS Remote file name in
// These two data usually need to be written into the database
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();
}
}
}
}

Step five , Write the download method
public static void download() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
// Load profile , The purpose is to get all TrackerServer Address information for
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
// establish Storage Client object of , You need to use this object to operate FastDFS, Upload, download and delete files
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();
}
}
}

Step six , Write deletion method
public static void delete() {
TrackerServer ts = null;
StorageServer ss = null;
StorageClient sc = null;
try {
// Load profile , The purpose is to get all TrackerServer Address information for
ClientGlobal.init("fastdfs.conf");
TrackerClient tc = new TrackerClient();
ts = tc.getTrackerServer();
ss = tc.getStoreStorage(ts);
// establish Storage Client object of , You need to use this object to operate FastDFS, Upload, download and delete files
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();
}
}
}

Step seven , To write main Method to test

After uploading, an array is returned , What's inside looks like this :
group1
M00/00/00/rBJzqmKgR4OACNHEABeAcCmFn1g961.png
Let's go to the /00/00/ Look inside the folder :
Delete method , Pass in the group name and file path , You will find that the files in the server have been deleted ,
The download method passes in the group name and file path and local path , You can find that the download is successful :

Warehouse address :
边栏推荐
- 福昕PDF编辑器v10.1.8绿色版
- 网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
- Huawei ENSP simulator enables devices of multiple routers to access each other
- 杰理之AD 系列 MIDI 功能说明【篇】
- VIM asynchronous problem
- Huawei ENSP simulator realizes communication security (switch)
- 伦敦银走势图分析的新方法
- Some suggestions for interface design
- acwing 3302. 表达式求值
- 【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
猜你喜欢

接口設計時的一些建議

Huawei ENSP simulator configures ACL access control list

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system

【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”

Leetcode+ 81 - 85 monotone stack topic

数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?

shp数据制作3DTiles白膜

Foxit pdf editor v10.1.8 green version

From automation to digital twins, what can Tupo do?

分析伦敦银走势图的技巧
随机推荐
吐槽 B 站收费,是怪它没钱么?
Day24:文件系统
分析伦敦银走势图的技巧
Browser render page pass
[Shenbo introduction] VI How to contact your favorite doctoral tutor
HMS Core 统一扫码服务
D3.js+Three.js数据可视化3d地球js特效
LeetCode 7. Integer inversion
Jmeter 之压测入门
Embedded TC test case
杰理之增加进关机前把触摸模块关闭流程【篇】
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
render函数与虚拟dom
UTF encoding and character set in golang
华为ensp模拟器 给路由器配置DHCP
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
ACM组合计数入门
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
黄金k线图中的三角形有几种?
扩展你的KUBECTL功能