FastDFS使用教程

FastDFS服务端

搭建FastDFS文件服务器
如果搭建服务器推荐地址csdn
https://blog.csdn.net/prcyang/article/details/89946190

FastDFS客户端

1.导入maven

<dependency>
	<groupId>com.github.tobato</groupId>
	<artifactId>fastdfs-client</artifactId>
	<version>1.26.1-RELEASE</version>
</dependency> 

2.配置yml源代码

fdfs:
  so-timeout: 1500001
  connect-timeout: 600001
  thumb-image: # 缩略图
    width: 60
    height: 60
  tracker-list: # tracker地址
    - xxx.xxx.xxx.xxx:22122
  view-ip: http://xxx.xxx.xxx.xxx/

3.导入重复注入配置类

package com.xxx.xxx.xxx;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

4.工具类配置

package com.xxx.upload.util;

import com.github.tobato.fastdfs.service.DefaultFastFileStorageClient;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

@Service
public class FastDFSUtil implements ApplicationContextAware {

        @Autowired
        private ApplicationContext applicationContext;

        //获取一个命令
        public FastFileStorageClient getClient() {
            return (DefaultFastFileStorageClient) this.applicationContext.getBean(DefaultFastFileStorageClient.class); //
        }

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;//获得该ApplicationContext引用
        }
}

5.使用样例

public String uploadFile(MultipartFile file){
	 try {
        result = "";
        InputStream inputStream = file.getInputStream();
        String md5Hex = DigestUtils.md5Hex(inputStream);
        String pathByMD5 = fileMd5RecordService.getPathByMD5(md5Hex);
        if (pathByMD5 != null){
            result = pathByMD5;
        }else{
            //上传文件
            FastFileStorageClient fastFileStorageClient = fastDFSUtil.getClient();
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
            // 返回路径
            result = storePath.getFullPath();
        }
     } catch (Exception e) {
         e.printStackTrace();
     }
}

本文章只是一个简单的Demo项目搭建,如果更多使用FastDFS,作者找到一个地址:
https://blog.csdn.net/qq_37128049/article/details/98885201
有更好的有参考价值;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

故里明月

感谢大大的打赏,俺会继续努力的

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

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

打赏作者

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

抵扣说明:

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

余额充值