1.加入fastdfs依赖:
<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
2.在启动类中导入配置:
@Import(FdfsClientConfig.class)
3.配置application.yml文件
fdfs:
#读取时间
so-timeout: 1500
#连接超时时间
connect-timeout: 600
#连接池
pool:
jmx-enabled: false
#缩略图
thumb-image:
width: 100
height: 100
#跟踪器配置地址(根据个人虚拟机地址+tracker端口号)
tracker-list: 192.168.157.129:22122
4.配置测试类:
@SpringBootTest
public class FastdfsApplicationTests {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Test
void contextLoads() throws FileNotFoundException {
//创建文件io流输入本地图片地址
File file=new File("D://Movies-Poster_Dataset/Images/tt0085121.jpg");
FileInputStream fileInputStream=new FileInputStream(file);
String fileName=file.getName();
//返回***.jpg等格式最后一个.后的字符串
String styleName=fileName.substring(fileName.lastIndexOf(".")+1);
StorePath storePath=fastFileStorageClient.uploadImageAndCrtThumbImage(fileInputStream,file.length(),styleName,null);
//输出组名
System.out.println(storePath.getGroup());
//输出文件名
System.out.println(storePath.getPath());
//输出全名
System.out.println(storePath.getFullPath());
}
}
5.启动各服务(详情见之前写的Nginx+FastDFS分布式文件系统搭建那篇文章):
- 启动tracker:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
- 启动storage节点服务:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
- 启动nginx: 先进入nginx的sbin目录下:
cd /usr/local/nginx/sbin
然后启动nginx服务:./nginx
6.启动测试类:
控制台中分别打印出组名、文件名和(组+文件名)的全名。
将全名输入在虚拟机的ip地址后,即可通过nginx访问到存放到虚拟机中的图片。
注意:
FastDFS进行文件上传操作时是用不到nginx的,那么nginx有什么用呢?
nginx是用来让FastDFS具备http访问的能力,如下图所示:
想通过http直接访问fastdfs来查看存储是否成功是做不到的,需要通过Nginx作为http的服务器,通过配置Nginx与FastDFS的插件,来实现与FastDFS的连接访问。