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详解