FastDFS-Client使用


FastDFS-Client使用步骤

一、准备依赖

fastdfs-client在maven官网是查找不到所对应的依赖的,我们需要先找到fastdfs-client的maven项目,然后通过install命令,导入到本地工厂,在引入此依赖即可在这里插入图片描述

二、使用步骤

1.加载配置文件

//1.读取配置文件
ClientGlobal.init(System.getProperty("user.dir")+"\\src\\main\\resources\\tracker.conf");

System.getProperty(“user.dir”)是获取本项目的绝对路径
配置文件内容:

tracker_server = 图片服务器的ip:22122

22122为图片服务器的默认端口

2.获取对应服务

 //2.声明大哥的客户端
 TrackerClient tc = new TrackerClient();
 //3.创建大哥的service
 TrackerServer ts = tc.getConnection();
 //声明小弟的服务
 StorageServer ss = null;
 //创建小弟的客户端
 StorageClient sc = new StorageClient(ts,ss);
 //上传文件
 String[] files = sc.upload_appender_file(pic.getBytes(), StringUtils.substringAfterLast(originalFilename, "."), null);

upload_appender_file三个参数:
1.图片的字节数组,springmvc获取图片上传项,可以直接获取到
2.图片后缀名(不包含.)
3.元信息,暂时没有,写的null

返回值files :
会返回两个长度的字符串数组,files[0]为图片服务器存储图片的组名
files[1]为具体的存放路径。

group1组名
M00/00/00/wKgloV78JPKEIPvXAAAAAFK3LxE506.png具体路径

在浏览器使用:

http://ip/group1/M00/00/00/wKgloV78JPKEIPvXAAAAAFK3LxE506.png

即可访问图片服务器存储的图片

猜你喜欢

转载自blog.csdn.net/weixin_43431123/article/details/111877075
今日推荐