SpringBoot项目优雅的整合 FastDFS

本文介绍了如何在SpringBoot项目中优雅地整合FastDFS文件存储系统,提供了详细的步骤和参考资料,帮助开发者实现高效稳定的文件上传和管理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 增加Maven依赖:

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.5</version>
</dependency>
FastDFS client git:https://github.com/tobato/FastDFS_Client 
yml文件中加入
fdfs:
  soTimeout: 1500
  connectTimeout: 600
  resHost: 127.0.0.1
  storagePort: 80
  serverPath: path
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    -  127.0.0.1:22122    #多个继续增加一行即可 携带 -

 

package com.configure;

import com.github.tobato.fastdfs.FdfsClientConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;


@Data
@Component
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@ConfigurationProperties(prefix = "fdfs")
public class FastDFSProperties {
    private String resHost;
    private String storagePort;
    private String serverPath;
}

package com.configure;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(FastDFSProperties.class)
public class FastDFSConfiguration {

}
package com.fastdfs;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.configure.FastDFSProperties;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;



@Component
public class FastDFSClientWrapper {

    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private FastDFSProperties fastDFSProperties;

    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return getResAccessUrl(storePath);
    }

    /**
     * 封装文件完整URL地址
     * @param storePath
     * @return
     */
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl =  "/"+ fastDFSProperties.getServerPath()+"/" + storePath.getFullPath();
        return fileUrl;
    }
}
package com.controller;

import com.google.common.collect.ImmutableMap;
import com.fastdfs.FastDFSClientWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

@RestController
@RequestMapping("/store")
@CrossOrigin(maxAge = 3600L)
public class FastDFSController {
    @Autowired
    private FastDFSClientWrapper dfsClient;

    @PostMapping("/fdfs_upload")
    public Object fdfsUpload(@RequestParam("file") MultipartFile file) {
        if(file.isEmpty()){
            return "File is Empty";
        }
        try {
            String fileUrl= dfsClient.uploadFile(file);
            return fileUrl;
        } catch (IOException e) {
            e.printStackTrace();
            return "Error";
        }
    }
}

参考文章:

https://www.2cto.com/kf/201803/727053.html

CentOS7  FastDFS服务器搭建推荐(很详细):

https://www.cnblogs.com/chiangchou/p/fastdfs.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值