JDK17 + SpringBoot3.5 整合 fastdfs-client-java1.29


一、pom.xml

GitHub - tobato/FastDFS_Client 要求 JDK1.8,因此只能使用 GitHub - happyfish100/fastdfs-client-java
这个是官方发布的依赖:

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.29</version>
</dependency>

如果上面的拉不下来,可以改用阿里云 Maven 公共仓库的:

        <dependency>
            <groupId>cn.aghost</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.29.0</version>
        </dependency>

二、配置类

为了将 FastDFS 的配置整合到 SpringBoot 的配置文件中,本文没有使用 fdfs_client.conf 或 fastdfs-client.properties 文件,而是定义了配置类。

application-dev.yml:

# 自定义 FastDFS 配置
fastdfs:
  connect_timeout_in_seconds: 5
  network_timeout_in_seconds: 30
  charset: UTF-8
  tracker_servers: ${linux-ip}:22122
  connection_pool:
    enabled: true
    max_count_per_entry: 500
    max_idle_time: 3600
    max_wait_time_in_ms: 3000

FastDFSProperties.java:

package com.example.demo.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "fastdfs")
@Data
public class FastDFSProperties {

    private String connectTimeoutInSeconds;

    private String networkTimeoutInSeconds;

    private String charset;

    private String trackerServers;

    private ConnectionPool connectionPool;

    @Data
    public static class ConnectionPool {
        private String enabled;
        private String maxCountPerEntry;
        private String maxIdleTime;
        private String maxWaitTimeInMs;
    }

}

FastDFSConfig.java:

package com.example.demo.configuration;

import com.example.demo.properties.FastDFSProperties;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.TrackerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.Properties;

@Slf4j
@Configuration
public class FastDFSConfig {

    @Autowired
    private FastDFSProperties fastDFSProperties;

    @Bean
    public TrackerClient getTrackerClient() throws IOException, MyException {
        log.info(String.valueOf(fastDFSProperties));

        Properties properties = new Properties();
        properties.setProperty("fastdfs.connect_timeout_in_seconds", fastDFSProperties.getConnectTimeoutInSeconds());
        properties.setProperty("fastdfs.network_timeout_in_seconds", fastDFSProperties.getNetworkTimeoutInSeconds());
        properties.setProperty("fastdfs.charset", fastDFSProperties.getCharset());
        properties.setProperty("fastdfs.tracker_servers", fastDFSProperties.getTrackerServers());
        properties.setProperty("fastdfs.connection_pool.enabled", fastDFSProperties.getConnectionPool().getEnabled());
        properties.setProperty("fastdfs.connection_pool.max_count_per_entry", fastDFSProperties.getConnectionPool().getMaxCountPerEntry());
        properties.setProperty("fastdfs.connection_pool.max_idle_time", fastDFSProperties.getConnectionPool().getMaxIdleTime());
        properties.setProperty("fastdfs.connection_pool.max_wait_time_in_ms", fastDFSProperties.getConnectionPool().getMaxWaitTimeInMs());

        ClientGlobal.initByProperties(properties);
        return new TrackerClient();
    }

}

三、实现上传、下载、删除

FastDFSService.java:

package com.example.demo.service;

public interface FastDFSService {

    String uploadFile(String fileName);

    int deleteFile(String fileId);

    int downloadFile(String fileId, String localFileName);

}

FastDFSServiceImpl.java:

package com.example.demo.service.impl;

import com.example.demo.service.FastDFSService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class FastDFSServiceImpl implements FastDFSService {

    @Autowired
    private TrackerClient trackerClient;

    /**
     * 获取StorageClient1
     */
    private StorageClient1 getStorageClient1() {
        try {

            TrackerServer trackerServer = trackerClient.getTrackerServer();
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            return new StorageClient1(trackerServer, storageServer);

        } catch (Exception e) {
            throw new RuntimeException("FastDFS获取连接失败", e);
        }

    }

    /**
     * 关闭StorageClient1
     */
    private void close(StorageClient1 storageClient1) {
        if (storageClient1 == null) {
            return;
        }
        try {
            storageClient1.close();
        } catch (IOException e) {
            log.error("FastDFS关闭连接失败", e);
        }
    }

    /**
     * 上传文件
     */
    @Override
    public String uploadFile(String fileName) {
        StorageClient1 storageClient1 = null;
        try {
            storageClient1 = getStorageClient1();
            return storageClient1.upload_file1(fileName, FilenameUtils.getExtension(fileName), null);
        } catch (Exception e) {
            log.error("FastDFS上传文件失败", e);
            return null;
        } finally {
            close(storageClient1);
        }
    }

    /**
     * 删除文件
     * 0表示成功,非0表示失败
     */
    @Override
    public int deleteFile(String fileId) {
        StorageClient1 storageClient1 = null;
        try {
            storageClient1 = getStorageClient1();
            return storageClient1.delete_file1(fileId);
        } catch (Exception e) {
            log.error("FastDFS删除文件失败", e);
            return -1;
        } finally {
            close(storageClient1);
        }
    }

    /**
     * 下载文件
     * 0表示成功,非0表示失败
     */
    @Override
    public int downloadFile(String fileId, String localFileName) {
        StorageClient1 storageClient1 = null;
        try {
            storageClient1 = getStorageClient1();
            return storageClient1.download_file1(fileId, localFileName);
        } catch (Exception e) {
            log.error("FastDFS下载文件失败", e);
            return -1;
        } finally {
            close(storageClient1);
        }
    }

}

参考:
[1]: GitHub - tobato/FastDFS_Client
[2]: GitHub - happyfish100/fastdfs-client-java
[3]: 阿里云云效Maven
[4]: FastDFS安装步骤以及SpringBoot集成fastdfs-client-java
[5]: java应用整合fastdfs实现文件 上传及下载

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐