fastDSF的实现文件上传,下载和删除
0.配置fastDSF的docker容器
虚拟机中拉取镜像
docker pull morunchang/fastdfs
运行tracker
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
运行storage
docker run -d --name storage --net=host -e TRACKER_IP=192.168.211.136:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
说明:
使用的网络模式是–net=host, 192.168.211.136是宿主机的IP
group1是组名,即storage的组
如果想要增加新的storage服务器,再次运行该命令,注意更换 新组名
设置开启自启动(可以不做):
docker update --restart=always tracker
docker update --restart=always storage
修改nginx
Nginx在这里主要提供对FastDFS图片访问的支持,Docker容器中已经集成了Nginx,我们需要修改nginx的配置,进入storage的容器内部,修改nginx.conf
进入容器
docker exec -it storage /bin/bash
修改配置
vi /etc/nginx/conf/nginx.conf
1.导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
</parent>
<dependencies>
<!--fastdfs-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.环境搭建
- 在启动类上加上@Import(FdfsClientConfig.class)
- 在application.yaml中添加
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个,这个是配置集群的可以写多个ip地址,这个是连接tracker的地址
- 192.168.211.136:22122
3. 图片的上传和下载以及删除方法实现
package com.liron;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.beanutils.converters.ShortArrayConverter;
import org.apache.commons.io.IOUtils;
import org.hibernate.validator.internal.properties.Field;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.*;
@SpringBootTest
public class FastDfsTest {
@Resource
private FastFileStorageClient storageClient;
/**
* 文件上传
*/
@Test
public void upload() throws FileNotFoundException {
File file = new File("D:/图片/壁纸/Computer/006whqQZly1h8hebens8lj318g0q8dmq.jpg");
FileInputStream inputStream = new FileInputStream(file);
// 获取文件大小
long fileSize = file.length();
// 获取文件拓展名
String extName = StringUtils.getFilenameExtension(file.getName());
// 上传文件
StorePath storePath = storageClient.uploadFile(inputStream, fileSize, extName, null);
System.out.println("storePath.getFullPath() = " + storePath.getFullPath());
System.out.println("storePath.getPath() = " + storePath.getPath());
System.out.println("storePath.getGroup() = " + storePath.getGroup());
}
/**
* 文件的下载
*/
@Test
public void download() throws IOException {
String groupName = "group1";
String path = "M00/00/00/wKjTiGQLEReAV4bmAAHZKLLDTgg406.jpg";
byte[] bytes = storageClient.downloadFile(groupName, path, ins -> {
//获取字节数组
return IOUtils.toByteArray(ins);
});
// 获取输出流将文件写在磁盘上
FileOutputStream fileOutputStream = new FileOutputStream(new File("e:/abc.png"));
fileOutputStream.write(bytes);
}
/**
* 文件删除
*/
@Test
public void delete(){
String groupName = "group1";
String path = "M00/00/00/wKjTiGQLEReAV4bmAAHZKLLDTgg406.jpg";
storageClient.deleteFile(groupName,path);
}
}