SpringBoot集成FastDFS

 

FastDfsConfig
@Slf4j
@Configuration
public class FastDfsConfig {

    private static final String CONFIG_FILENAME = "fastdfs-client.properties";

    @Bean
    public TrackerClient trackerClient(){
        try {
            //全局初始化配置文件
            ClientGlobalPower.initByProperties(CONFIG_FILENAME);
            //定义TrackerClient,用于请求TrackerServer
            //连接tracker
            return new TrackerClient();
        } catch (Exception e) {
            log.error("加载fastDfs配置失败",e);
        }
        return null;
    }

}

 

FastDfsUtils
@Slf4j
@Component
public class FastDfsUtils {

    @Autowired
    private TrackerClient trackerClient;

    public StorageClient1 init(){
        try {
            //连接tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            ProtoCommon.activeTest(trackerServer.getSocket());
            //获取Stroage
            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
            //创建stroageClient
            ProtoCommon.activeTest(trackerServer.getSocket());
            return new StorageClient1(trackerServer,storeStorage);
        } catch (IOException e) {
            log.error("fastdfs ProtoCommon.activeTest初始化异常",e);
        }
        return null;
    }

    /**
     * 上传文件
     * @param bytes 文件
     * @param file_ext_name 扩展名
     * @return
     */
    public String upload(byte[] bytes,String file_ext_name){
        if (StringUtils.isEmpty(file_ext_name))
            return null;
        final StorageClient1 storageClient = init();
        if (storageClient != null){
            String fileId = null;
            try {
                fileId = storageClient.upload_file1(bytes, file_ext_name,null);
                return fileId;
            } catch (Exception e) {
                log.error("上传文件出现异常",e);
            }
        }
        return null;
    }

    /**
     * 删除文件
     * @param file_id
     * @return
     */
    public Integer delete(String file_id){
        int count = 0;
        final StorageClient1 storageClient = init();
        if (storageClient != null){
            try {
                count = storageClient.delete_file1(file_id);
                return count;
            } catch (Exception e) {
                log.error("删除文件出现异常,file_id:{}",file_id,e);
            }
        }
        return count;
    }


}

 

 

### Spring Boot 中集成 FastDFS 在 Spring Boot 项目中集成 FastDFS 可以通过引入相应的依赖并配置来实现文件存储功能。通常情况下,FastDFS 客户端库用于连接到 FastDFS 服务器集群。 #### 添加 Maven 或 Gradle 依赖项 对于基于 Maven 的构建工具,在 `pom.xml` 文件内添加如下依赖: ```xml <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency> ``` 而对于采用 Gradle 构建系统的项目,则应在 build.gradle 文件里加入下面的内容: ```groovy implementation 'org.csource:fastdfs-client-java:1.27' ``` #### 配置 FastDFS 属性 创建一个新的 Java 类用来加载 FastDFS 的客户端全局参数设置,并将其注册为 Bean 对象以便于后续操作使用。这一步骤可以通过编写一个名为 FdfsConfig.java 的类完成[^3]。 ```java @Configuration public class FdfsConfig { @Value("${fdfs.tracker_servers}") private String trackerServers; @Bean(initMethod = "init") public ClientGlobal fdfsClientGlobal() { ClientGlobal clientGlobal = new ClientGlobal(); try { Properties properties = new Properties(); properties.setProperty("tracker_server", trackerServers); // 设置其他必要的属性... clientGlobal.setGProperties(properties); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } return clientGlobal; } } ``` 上述代码片段展示了如何定义一个初始化方法 init 来读取 application.properties 或者 yml 文件中的配置信息给 FastDFS 使用。 #### 编写服务层逻辑 为了方便业务调用上传下载等功能,可以封装一层 Service 接口和服务实现类来进行具体的操作处理。这里提供了一个简单的 UploadService 示例供参考。 ```java @Service public class FileUploadService { @Autowired TrackerClient trackerClient; /** * 实现文件上传至 FastDFS 并返回访问路径. */ public String uploadFile(MultipartFile file){ StorageServer storageServer = null; TrackerServer trackerServer = null; try{ trackerServer = this.trackerClient.getConnection(); ProtoCommon ACTIVE_TEST_REQUEST = new ProtoCommon.ACTIVE_TEST_REQUEST(); byte[] bytes = file.getBytes(); StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer); String result=storageClient1.upload_file1(bytes,file.getContentType(),""); return result; }catch(Exception ex){ log.error(ex.toString()); throw new RuntimeException("Failed to upload the file."); } finally { if(storageServer!=null){ try{storageServer.close();}catch(IOException ignored){} } if(trackerServer != null){ try {trackerServer.close();} catch (IOException ignored) {} } } } } ``` 此部分实现了基本的文件上传流程,实际应用时可能还需要考虑更多细节如异常捕获、日志记录以及性能优化等方面的工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值