关于 FastDFS 的搭建安装可以看上一篇文章 FastDFS 安装配置
这里直接介绍用 SpringBoot 搭建 FastDFS 的测试 Demo。
搭建过程
新建一个 SpringBoot 项目, pom.xml 配置引入:
<!-- https://mvnrepository.com/artifact/cn.bestwu/fastdfs-client-java --> <dependency> <groupId>cn.bestwu</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
在 resources 目录下新建文件: fdfs_client.conf
,内容如下:
#连接tracker服务器超时时长 connect_timeout = 2 #socket连接超时时长 network_timeout = 30 #文件内容编码 charset = UTF-8 #tracker服务器端口 http.tracker_http_port = 8080 #http.anti_steal_token = no http.anti_steal.check_token=true http.secret_key = FastDFS1234567890 #tracker服务器IP和端口(可以写多个) #自行配置 tracker_server = [tracker_server_ip]:22122
新建类 FastDFSClient,作为核心类:
package top.honeybee.fastdfs;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* FastDFS文件上传下载工具类
*/
public class FastDFSClient {
//自行配置
private static final String CONFIG_FILENAME = "src/main/resources/fdfs_client.conf";
private static final String GROUP_NAME = "group1";
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient storageClient = null;
static{
try {
ClientGlobal.init(CONFIG_FILENAME);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
public FastDFSClient() throws Exception {
trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);;
storageClient = new StorageClient(trackerServer, storageServer);
}
/**
* 上传文件
* @param file 文件对象
* @param fileName 文件后缀名
* @return
*/
public String[] uploadFile(File file, String fileName) {
return uploadFile(file,fileName,null);
}
/**
* 上传文件
* @param file 文件对象
* @param fileName 文件名
* @param metaList 文件元数据
* @return
*/
public String[] uploadFile(File file, String fileName, Map<String,String> metaList) {
try {
byte[] buff = IOUtils.toByteArray(new FileInputStream(file));
NameValuePair[] nameValuePairs = null;
if (metaList != null) {
nameValuePairs = new NameValuePair[metaList.size()];
int index = 0;
for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String,String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
nameValuePairs[index++] = new NameValuePair(name,value);
}
}
return storageClient.upload_file(GROUP_NAME,buff,fileName,nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取文件元数据
* @param groupName 组名
* @param fileId 文件ID
* @return
*/
public Map<String,String> getFileMetadata(String groupname,String fileId) {
try {
NameValuePair[] metaList = storageClient.get_metadata(groupname,fileId);
if (metaList != null) {
HashMap<String,String> map = new HashMap<String, String>();
for (NameValuePair metaItem : metaList) {
map.put(metaItem.getName(),metaItem.getValue());
}
return map;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 删除文件
* @param groupName 组名
* @param fileId 文件ID
* @return 删除失败返回-1,否则返回0
*/
public int deleteFile(String groupname,String fileId) {
try {
return storageClient.delete_file(groupname,fileId);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/**
* 下载文件
* @param groupName 组名
* @param fileId 文件ID(上传文件成功后返回的ID)
* @param outFile 文件下载保存位置
* @return
*/
public int downloadFile(String groupName,String fileId, File outFile) {
FileOutputStream fos = null;
try {
byte[] content = storageClient.download_file(groupName,fileId);
fos = new FileOutputStream(outFile);
InputStream ips = new ByteArrayInputStream(content);
IOUtils.copy(ips,fos);
return 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return -1;
}
public static void main(String[] args) throws Exception {
FastDFSClient client = new FastDFSClient();
//测试文件上传
File file = new File("F:\\新建文件夹\\background3.jpg");
System.out.println(file.exists());
String[] result = client.uploadFile(file, "jpg");
System.out.println(result.length);
System.out.println(result[0]);
System.out.println(result[1]);
//测试文件下载
File file2 = new File("F:\\新建文件夹\\a.jpg");
System.out.println(client.downloadFile(result[0], result[1], file2));
//测试文件删除
System.out.println(client.deleteFile(result[0], result[1]));
}
}
执行函数返回:
基类已经测试成功啦,小伙伴们可以先去试试,也可以自行根据需求扩展接口...
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于