FastDFS是用c语言编写的一款开源的分布式文件系统,它是由淘宝资深架构师余庆编写并开源。FastDFS专为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很 容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务
第一步导入依赖
<!--FastDFS客户端-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>${fastDFS.client.version}</version>
</dependency>
<fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version>
第二步:编写配置类
@Configuration
@Import(FdfsClientConfig.class)
/*解决jmx重复注册bean的问题*/
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastclientImporter {
}
第三步:配置yml文件
fdfs:
so-timeout: 1501 # 读取时间
connect-timeout: 601 #连接超时时间
thumb-image: #缩略图
height: 60
width: 60
tracker-list: #tracker地址:你的虚拟机服务器地址+端口(默认是22122)
xxxxxx:22122 例如: 192.168.56.101:22122
第四步:进行测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
/*上传文件*/
@Test
public void testUplod() throws FileNotFoundException {
/*上传文件的文件*/
File file = new File("xxx路径//1.jpg");
/*上传并保存的图片:参数 1、上传的文件流 2、文件的大小 3、文件的后缀 4、设置*/
StorePath jpg = this.fastFileStorageClient.uploadFile(new FileInputStream(file), file.length(), "jpg", null);
//带分组的路径
System.out.println(jpg.getFullPath());
//不带分组的路径
System.out.println(jpg.getPath());
}
/*上传文件带缩略图*/
@Test
public void testUploadAndSUOLUETU() throws FileNotFoundException {
File file = new File("xxx路径//1.jpg");
StorePath jpg = this.fastFileStorageClient.uploadImageAndCrtThumbImage(new FileInputStream(file), file.length(), "png", null);
//带分组的路径
System.out.println(jpg.getFullPath());
//不带分组的路径
System.out.println(jpg.getPath());
//获取缩略图的路径
String thumbImagePath = thumbImageConfig.getThumbImagePath(jpg.getPath());
System.out.println(thumbImagePath);
}
}