fastdfs 测试客户端_Springboot 集成 FastDFS

这篇是接上一篇Docker搭建FastDFS服务器,只要写一下Springboot 集成 FastDFS 的流程。 陈伟,公众号:JAVA架构师圈子使用Docker 部署FastDFS

1. 创建一个springboot项目,在pom.xml添加fastdfs 依赖包, 这里的依赖版本引相对比较新的就行。

          <dependency>            <groupId>com.github.tobatogroupId>            <artifactId>fastdfs-clientartifactId>            <version>1.26.5version>        dependency>

2. 在application.yml 中配置 fastdfs相关配置,本人习惯将application.properties 改为application .yml , 配置项都差不多,当然生产环境中还需要配置更多的参数。。。

fdfs:  connect-timeout: 600  so-timeout: 1500  tracker-list: 139.129.229.34:22122

6d17a458212181337fa690473445981f.png

3. 写测试类:引入FastFileStorageClient FastDFS文件上传客户端,它提供了文件上传,下载,删除,查询等方法,感兴趣的小伙伴可以自行研究一下。我这里用springboot自带的测试类来给大家演示。

@SpringBootTestclass DemoApplicationTests {    @Autowired    private FastFileStorageClient fastFileStorageClient ;    @Test    void contextLoads() throws FileNotFoundException {        File file = new File("D://微信头像.jpg");        String fileName = file.getName();        String extName = fileName.substring(fileName.lastIndexOf(".") + 1);        FileInputStream inputStream = new FileInputStream(file);        StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), extName, null);        System.out.println(storePath.getFullPath());        System.out.println(storePath.getGroup());        System.out.println(storePath.getPath());    }}

287f519220854f6ec97dc735b82eba21.png

4. 运行测试类,返回上传文件的url: /group1/M00/00/00/rB-Pw19rH9-Ae0wGAAFwd8BgrrI894.jpg

f9ee7395aab5de3ae18d5295facdfa51.png

5. 拿到url, 访问文件    

http://IP:8888/group1/M00/00/00/rB-Pw19rH9-Ae0wGAAFwd8BgrrI894.jpg

注意:这里的ip换成自己的ip

2b2547499546c916351acefa57cc0594.png

6. 总结一下,首先添加fastdfs依赖,再配置fastdfs路径,最后使用FastFileStorageClient 提供的方法上传文件。springboot整合fastdfs测试项目完成。。。。

欢迎大家评价或者点赞!!!!大家想了解什么样的文章,也可以评论告知,后期会出一些SpringCloudAlibaba的文章,和ElasticSearch使用等相关文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值