一、搭建FastDFS
- 安装镜像、启动容器
docker pull morunchang/fastdfs
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
docker run -d --name storage --net=host -e TRACKER_IP=(宿主机ip地址):22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
- 开机启动设置
docker update --restart=always tracker
docker update --restart=always storage
- 开放端口
![在这里插入图片描述](https://img-blog.csdnimg.cn/9d04084026e94a4caaa18bb76f259d8c.png)
二、项目配置
- 引入依赖
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
- 在resources文件夹增加FastDFS配置文件:fdfs_client.conf文件
connect_timeout=60
network_timeout=60
charset=UTF-8
http.tracker_http_port=8080
tracker_server=(宿主机ip地址):22122
- 配置文件yml
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
application:
name: file
三、文件操作
- 文件信息类
public class FastDFSFile implements Serializable {
private String name;
private byte[] content;
private String ext;
private String md5;
private String author;
public FastDFSFile(String name, byte[] content, String ext, String md5, String author) {
this.name = name;
this.content = content;
this.ext = ext;
this.md5 = md5;
this.author = author;
}
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
public FastDFSFile() {
}
}
- 文件工具类
package com.changgou.util;
import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FastDFSClient {
static {
try {
String filePath = new ClassPathResource("fdfs_client.conf").getPath();
ClientGlobal.init(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String[] upload(FastDFSFile file){
NameValuePair[] meta_list = new NameValuePair[1];
meta_list[0] =new NameValuePair(file.getAuthor());
String[] uploadResults = null;
try {
StorageClient storageClient = getStorageClient();
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
} catch (Exception e) {
e.printStackTrace();
}
return uploadResults;
}
public static FileInfo getFile(String groupName, String remoteFileName){
try {
StorageClient storageClient = getStorageClient();
return storageClient.get_file_info(groupName,remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static InputStream downFile(String groupName, String remoteFileName){
try {
StorageClient storageClient = getStorageClient();
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
return new ByteArrayInputStream(fileByte);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void deleteFile(String groupName,String remoteFileName){
try {
StorageClient storageClient = getStorageClient();
storageClient.delete_file(groupName,remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static StorageServer getStorages(String groupName){
try {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
return trackerClient.getStoreStorage(trackerServer,groupName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ServerInfo[] getServerInfo(String groupName, String remoteFileName){
try {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getTrackerUrl(){
try {
TrackerServer trackerServer = getTrackerServer();
return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static TrackerServer getTrackerServer() throws IOException {
TrackerClient trackerClient=new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
return trackerServer;
}
public static StorageClient getStorageClient() throws IOException {
TrackerServer trackerServer = getTrackerServer();
StorageClient storageClient=new StorageClient(trackerServer,null);
return storageClient;
}
}
- 工具调用
@PostMapping("/upload")
public String upload(@RequestParam("file")MultipartFile file) throws IOException {
FastDFSFile fastDFSFile=new FastDFSFile(file.getOriginalFilename(),
file.getBytes(),
StringUtils.getFilenameExtension(file.getOriginalFilename()));
String[] uploads = FastDFSClient.upload(fastDFSFile);
return FastDFSClient.getTrackerUrl()+"/"+uploads[0]+"/"+uploads[1];
}
- 填写Headers,body,测试,复制返回连接打开即可看到原文件。
Key:Content-Type
Value:multipart/form-data
![在这里插入图片描述](https://img-blog.csdnimg.cn/911845f6194c4d3cba1b124b85ab2e4e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y-y5YaF5YWLSg==,size_20,color_FFFFFF,t_70,g_se,x_16)
四、总结
- 搭建FastDFS,开放端口。
- 引入依赖,编写application配置文件,编写FastDFS配置文件,编写文件信息类,文件工具类。
- 调用工具类方法进行上传等文件操作。