1, 下载官方的源代码: https://codeload.github.com/happyfish100/fastdfs-client-java/zip/master
2, 采用 maven 命令编译成 jar 安装到本地 maven 库: mvn clean install
3, 在 Java 程序中使用它提供的 API 来访问 fastDFS 文件系统;
注: maven 官网的客户端有点问题, 需要自己生成 jar 包
执行 mvn clean install 后, 会出现在本地仓库中
测试代码如下
工具类
- package com.topcheer.fastdfs;
- ?
- import java.io.IOException;
- ?
- 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;
- ?
- public class FastDfsUtil {
- ?
- private static final String conf = "fastdfs-client.conf";
- private static TrackerServer trackerServer = null;
- private static StorageServer storageServer = null;
- ?
- public static StorageClient getStorageClient() {
- StorageClient storageClient = null;
- try {
- // 1 初始化配置文件
- ClientGlobal.init(conf);
- ?
- // 2 创建 tracker 的客户端对象
- TrackerClient client = new TrackerClient();
- ?
- // 3 创建 tracker 的服务器对象
- trackerServer = client.getConnection();
- ?
- // 4 创建 storage 的客户端对象
- storageServer = client.getStoreStorage(trackerServer);
- ?
- // 5 创建 storage 的服务器对象
- storageClient = new StorageClient(trackerServer, storageServer);
- ?
- } catch (Exception e) {
- e.printStackTrace();
- }
- return storageClient;
- }
- ?
- // 关闭资源
- public static void close() {
- ?
- if (trackerServer != null) {
- try {
- trackerServer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- ?
- if (storageServer != null) {
- try {
- storageServer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- ?
- }
- ?
- }
fastdfs-client.conf 配置信息
tracker_server = 192.168.180.104:22122
上传
- public static void uploadFile() {
- try {
- String[] strings = FastDfsUtil.getStorageClient().upload_appender_file("C:\\Users\\asus\\Desktop\\ 激活码. txt", "txt", null);
- Arrays.asList(strings).forEach(s -> System.out.println(s));
- //group1
- // M00/00/00/wKi0aF3II2yEPlv6AAAAAHp_ifU838.txt
- } catch (IOException e) {
- e.printStackTrace();
- } catch (MyException e) {
- e.printStackTrace();
- }finally {
- FastDfsUtil.close();
- }
- }
注: nginx.conf 的端口被改成 8888 了, 测试结果如图
下载
- public static void downfile() {
- ?
- try {
- ?
- FastDfsUtil.getStorageClient().download_file("group1", "M00/00/00/wKi0aF3II2yEPlv6AAAAAHp_ifU838.txt", "d:/a.txt");
- } catch (IOException e) {
- e.printStackTrace();
- } catch (MyException e) {
- e.printStackTrace();
- }finally {
- FastDfsUtil.close();
- }
- }
删除
- /**
- * delete file from storage server
- *
- * @param group_name the group name of storage server
- * @param remote_filename filename on storage server
- * @return 0 for success, none zero for fail (error code)
- */
- public static void delFile() {
- try {
- int file = FastDfsUtil.getStorageClient().delete_file("group1", "M00/00/00/wKi0aF3II2yEPlv6AAAAAHp_ifU838.txt");
- System.out.println(file);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (MyException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- FastDfsUtil.close();
- }
- }
来源: http://www.bubuko.com/infodetail-3282353.html