栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

FastDFS使用教程

FastDFS使用教程

FastDFS

FastDFS服务端FastDFS客户端

1.导入maven2.配置yml源代码3.导入重复注入配置类4.工具类配置5.使用样例

FastDFS服务端
搭建FastDFS文件服务器
如果搭建服务器推荐地址csdn
https://blog.csdn.net/prcyang/article/details/89946190
FastDFS客户端 1.导入maven

	com.github.tobato
	fastdfs-client
	1.26.1-RELEASE
 
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
有更好的有参考价值;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/761664.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号