SpringBoot集成FastDFS的配合

7 篇文章 0 订阅
6 篇文章 0 订阅

最近做的项目需要把相关的录音文件上传到FastDFS服务器,因为之前没有做过,所以都是在网上找一些资源做参考,最后经过调试,终于可以上传成功了,接下来我来和大家分享我写的相关代码,其他就不讲解了,希望能做到大家参考着也能配置成功,在项目中使用:

1、首先添加maven的依赖

        <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27.0.0</version>
        </dependency>

2、添加配置文件:

文件内容:

fastdfs.connect_timeout_in_seconds=30

fastdfs.network_timeout_in_seconds=60

fastdfs.charset=UTF-8

fastdfs.http_tracker_http_port=8888

fastdfs.tracker_servers=127.0.01:22122

3、程序读取:

首先yml配置相关的配置项:

fdfs:
  trackerweb: http://127.0.0.1:8888/
  storage:
    serverip: 127.0.0.2
    serveripport: 23000
    storagepath: 0
  env: -test
  configFile: /data/fdfs_client-test.properties

然后添加相关的配置类:

@Configuration
@Slf4j
public class FastDFSConfig {

    @Value("${fdfs.storage.serverip}")
    private String storageServerIp;

    @Value("${fdfs.storage.serveripport}")
    private Integer storageServerPort;

    @Value("${fdfs.storage.storagepath}")
    private Integer storageServerPath;

    @Value("${fdfs.env}")
    private String env;

    @Value("${fdfs.configFile}")
    private String configFile;

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageServer storageServer = null;
    private StorageClient1 storageClient = null;

    @PostConstruct
    public void init() throws Exception {
//        String path = "fdfs_client" + env + ".properties";
//        Resource resource = new ClassPathResource(path);
//        String conf = resource.getFile().getCanonicalPath();
//        ClientGlobal.init(conf);

        Properties properties = new Properties();
        Resource resource = new FileSystemResource(configFile);
        properties.load(resource.getInputStream());
        ClientGlobal.initByProperties(properties);

        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = new StorageServer(storageServerIp,storageServerPort,storageServerPath);
        storageClient = new StorageClient1(trackerServer, storageServer);
        log.info("------ FastDFS配置加载完毕 ------");
    }

    @Bean
    public StorageClient storageClient(){
        return storageClient;
    }
}

其中加载配置文件分环境和读取文件系统两种方式,项目是springboot,我采用读取文件系统的方式读取配置文件,根据不同的项目架构来调整;

4、程序中来调用:

@Service
@Slf4j
public class FastDfsService {

    @Resource
    private StorageClient1 storageClient;

    /**
     * 上传文件
     * @return
     */
    public String uplaodFile(String hlFileUrl) {

        log.info(" ------ 语音文件路径:【{}】,执行上传操作 ------",hlFileUrl);

        String fileName = FilenameUtils.getName(hlFileUrl);
        String extensionName = FilenameUtils.getExtension(hlFileUrl);

        log.info(" ------ 语音文件名称:【{}】, 后缀名称:【{}】 ------",fileName,extensionName);

        InputStream inputStream = null;

            try {

                URL url = new URL(hlFileUrl);

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setDoInput(true);

                conn.setRequestMethod("GET");

                inputStream = conn.getInputStream();

                byte[] fileContent = IOUtils.toByteArray(inputStream);

                String fastFullPath =  storageClient.upload_file1(fileContent,extensionName,null);

//                String fastFullPath =  null;

                log.info(" ------ 语音文件:【{}】,上传FastDfs路径:【{}】 ------",fileName,fastFullPath);

                log.info(" ------ 语音文件:【{}】,上传成功 ------",fileName);

                return fastFullPath;

            }catch (Exception e){

                e.printStackTrace();

                log.info(" ------ 语音文件:【{}】,上传fastDfs异常:{} ------",fileName,e);
            }finally {
                try {
                    if (inputStream != null){
                        inputStream.close();
                    }
                }catch (IOException e){
                }

            }

        return null;
    }

}

这样就完成了相关的上传功能,还有其他的一些下载删除等,可以到网上搜索相关的API,本文不做更多的代码分享,希望我的内容给大家一些思路。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门主冬七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值