介绍
本文讲述如何通过java调用FastDFS的文件上传、下载及删除,提供示例代码。
编译fastdfs-client-java
需要环境git、jdk8、maven
git clone https://github.com/happyfish100/fastdfs-client-java.git
cd fastdfs-client-java/
mvn package
配置
target目录中就会产生fastdfs-client-java-x.jar文件
复制该文件到项目中的src/main/resources/lib目录中
添加maven依赖
<dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/fastdfs-client-java-1.27-SNAPSHOT.jar</systemPath><version>1.27-SNAPSHOT</version></dependency>
resources配置目录下添加配置文件fdfs_client.conf
tracker_server=192.168.81.143:22122
#注意ip和端口要指向服务器的fastdfs服务,如果有防火墙的要做好开放处理
代码实现
-------FastDFSHelper.java
import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;@Slf4j
public final class FastDFSHelper {private static TrackerClient trackerClient;static {try {ClientGlobal.init("fdfs_client.conf");trackerClient = new TrackerClient();} catch (IOException | MyException e) {log.error("error", e);}}/*** 向FastDFS上传文件** @param localFilename 本地文件名* @return 上传成功,返回组名和该文件在FastDFS中的名称;上传失败,返回null*/public static String[] uploadFile(String localFilename) {TrackerServer trackerServer;try {trackerServer = trackerClient.getConnection();} catch (IOException e) {log.error("error", e);return null;}StorageClient storageClient = new StorageClient(trackerServer, null);try {String[] arr = storageClient.upload_file(localFilename, null, null);if (arr == null || arr.length != 2) {log.error("向FastDFS上传文件失败");} else {log.info("向FastDFS上传文件成功");log.info("id:{}/{}",arr);return arr;}} catch (IOException | MyException e) {log.error("error", e);} finally {closeTrackerServer(trackerServer);}return null;}/*** 从FastDFS下载文件** @param localFilename 本地文件名* @param groupName 文件在FastDFS中的组名* @param remoteFilename 文件在FastDFS中的名称*/public static boolean downloadFile(String localFilename, String groupName, String remoteFilename) {TrackerServer trackerServer;try {trackerServer = trackerClient.getConnection();} catch (IOException e) {log.error("error", e);return false;}StorageClient storageClient = new StorageClient(trackerServer, null);File file = new File(localFilename);try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {byte[] content = storageClient.download_file(groupName, remoteFilename);if (content == null || content.length == 0) {log.error("文件大小为空!");boolean flag = file.delete();log.info("删除文件结果:{}", flag);return false;}bos.write(content);log.info("成功下载文件: " + localFilename);return true;} catch (IOException | MyException e) {log.error("error", e);} finally {closeTrackerServer(trackerServer);}return false;}/*** 从FastDFS删除文件** @param groupName 文件在FastDFS中的组名* @param remoteFilename 文件在FastDFS中的名称*/public static boolean deleteFile(String groupName, String remoteFilename) {TrackerServer trackerServer;try {trackerServer = trackerClient.getConnection();} catch (IOException e) {log.error("error", e);return false;}StorageClient storageClient = new StorageClient(trackerServer, null);try {int i = storageClient.delete_file(groupName, remoteFilename);if (i == 0) {log.info("FastDFS删除文件成功");return true;} else {log.info("FastDFS删除文件失败");return false;}} catch (IOException | MyException e) {log.error("error", e);} finally {closeTrackerServer(trackerServer);}return false;}private static void closeTrackerServer(TrackerServer trackerServer) {try {if (trackerServer != null) {log.info("关闭trackerServer连接");trackerServer.close();}} catch (IOException e) {log.error("error", e);}}
}
----------FastdfsdemoApplication.java
public class FastdfsdemoApplication {public static void main(String[] args) {String[] arr= FastDFSHelper.uploadFile("/home/whq/a.txt");if(arr!=null){boolean result= FastDFSHelper.downloadFile("/home/whq/a2.txt",arr[0],arr[1]);boolean result2=FastDFSHelper.deleteFile(arr[0],arr[1]);}}
}
参考资料
https://blog.csdn.net/csdlwzy/article/details/83309831