当前位置:网站首页>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 :
边栏推荐
- admas零件名重复
- PS vertical English and digital text how to change direction (vertical display)
- Quelques suggestions pour la conception de l'interface
- Huawei ENSP simulator realizes communication security (switch)
- 华为ensp模拟器 三层交换机
- 伦敦银走势图分析的新方法
- [1200. Minimum absolute difference]
- 宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
- Huawei simulator ENSP common commands
- LeetCode+ 81 - 85 单调栈专题
猜你喜欢

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

torch.tensor和torch.Tensor的区别

【1200. 最小絕對差】
![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Minimum absolute difference]

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

杰理之AD 系列 MIDI 功能说明【篇】

The concept and application of hash table

uniapp 富文本编辑器使用

render函数与虚拟dom

y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
随机推荐
Record the online bug solving list (unfinished to be continued 7/4)
Redis:Redis配置文件相关配置、Redis的持久化
网络命名空间
Gobang go to work fishing tools can be LAN / man-machine
Vue cleans up the keepalive cache scheme in a timely manner
Procurement in software development
BFC interview Brief
LeetCode 8. String conversion integer (ATOI)
RFID仓储管理系统解决方案的优点
记一次重复造轮子(Obsidian 插件设置说明汉化)
Jekins initialization password not found or not found
华为ensp模拟器 实现多个路由器的设备可以相互访问
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
Huawei simulator ENSP common commands
[1200. Minimum absolute difference]
PS竖排英文和数字文字怎么改变方向(变竖直显示)
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
插入排序,选择排序,冒泡排序
Quelques suggestions pour la conception de l'interface
基于OpenCV haarcascades的对象检测