springboot集成FastDFS

本文详细介绍了如何在SpringBoot项目中集成FastDFS,包括添加依赖、导入配置、配置YML文件,以及解决在集成过程中遇到的防火墙问题,最终实现通过获取的地址访问图片资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot集成FastDFS

1.添加依赖
新建一个springboot项目,在pom.xml文件中导入fastDFS客户端依赖,
在这里插入图片描述
2.导入配置
在启动类中,添加@Import(FdfsClientConfig.class)
在这里插入图片描述
3.配置application.yml
在这里插入图片描述
4.编程


package com.learn;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SpringBootTest
class FastdfstestApplicationTests {

	@Autowired
	private FastFileStorageClient fastFileStorageClient;

	@Test
	void contextLoads() throws FileNotFoundException {

		//创建文件输入流
		File file = new File("d://dog.jpg");
		String name = file.getName();
		String lastName = name.substring(name.lastIndexOf(".")+1);
		FileInputStream inputStream = new FileInputStream(file);

		StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), lastName, null);
		System.out.println(storePath.getGroup());
		System.out.println(storePath.getPath());

	}

}

报错:com.github.tobato.fastdfs.exception.FdfsConnectException: 无法获取服务端连接资源:can't create connection to/192.168.1.103:22122

解决:防火墙问题(超过两台主机之间进行交互会出现的问题)
进入文件:[root@192 sbin]# vi /etc/sysconfig/iptables
在这里插入图片描述
将22122端口添加进去。

重启防火墙:systemctl restart iptables

再次运行程序:
报错:com.github.tobato.fastdfs.exception.FdfsConnectException: 无法获取服务端连接资源:can't create connection to/192.168.1.103:23000

原因;需要打开在防火墙中打开存储节点的端口号
在这里插入图片描述
重启防火墙 systemctl restart iptables (centos7 使用)

运行程序:
在这里插入图片描述
ok! 完美!
通过访问获取到的地址去访问图片资源:http://192.168.1.103/group1/M00/00/00/wKgBZ16sSGSAeZHpAAFuVcirLuA251.jpg
在这里插入图片描述
这个小狗是我的手机壁纸,超好看。。。。。如有侵权,请联系删除!!!!

上诉操作需要在linux中安装配置FastDFS的情况下才能成功, 具体参照:安装FastDFS详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值