这篇是接上一篇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
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()); }}
4. 运行测试类,返回上传文件的url: /group1/M00/00/00/rB-Pw19rH9-Ae0wGAAFwd8BgrrI894.jpg
5. 拿到url, 访问文件
http://IP:8888/group1/M00/00/00/rB-Pw19rH9-Ae0wGAAFwd8BgrrI894.jpg
注意:这里的ip换成自己的ip
6. 总结一下,首先添加fastdfs依赖,再配置fastdfs路径,最后使用FastFileStorageClient 提供的方法上传文件。springboot整合fastdfs测试项目完成。。。。
欢迎大家评价或者点赞!!!!大家想了解什么样的文章,也可以评论告知,后期会出一些SpringCloudAlibaba的文章,和ElasticSearch使用等相关文章。