Spring Boot2 集成FastDFS

什么是FastDFS?

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

相信大家既然是要整合FastDFS 那应该对它有个基础的认识吧

既然要整合FastDFS那就要安装FastDFS服务,安装FastDFS系统时会配置相应的端口,后续请求文件系统时会用到,具体配置大家百度如何安装配置FastDFS,这里就不介绍如何安装FastDFS服务了。

以下是Spring Boot2 整合FastDFS步骤

1.pom引入相关依赖包


<!--fstdfs client-->
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.5</version>
</dependency>

2.增加fastdfs相关配置

application.yml中增加属性

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #缩略图生成参数
    width: 200
    height: 200
  tracker-list:            #TrackerList参数,支持多个
    - 192.168.1.178:22122

3.注入FdfsClientConfig类


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

4.编写fastdfs工具类封装相关操作FastdfsClientUtil


package org.guajava.common.utils.fastdfs;
 
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.tianshun.common.utils.StringUtils;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * Fastdfs工具类
 */
@Component
public class FastdfsClientUtil {
 
   private final Logger logger = LoggerFactory.getLogger(FastdfsClientUtil.class);
   @Autowired
   private FastFileStorageClient storageClient;
   @Autowired
   private ThumbImageConfig thumbImageConfig;
 
 
   //上传文件
   public String upload(MultipartFile myfile) throws Exception{
      //文件名
      String originalFilename = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf(".") + 1);
      // 文件扩展名
      String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
 
      StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(myfile.getInputStream(), myfile.getSize(),originalFilename , null);
 
      String path = storePath.getFullPath();
 
      return path;
   }
   
   /**
    * 删除文件
    * @Param fileUrl 文件访问地址
    */
   public void deleteFile(String fileUrl) {
      if (StringUtils.isEmpty(fileUrl)) {
         return;
      }
      try {
         StorePath storePath = StorePath.parseFromUrl(fileUrl);
         storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
      } catch (FdfsUnsupportStorePathException e) {
         logger.warn(e.getMessage());
      }
   }
}

5.编写测试类,测试上传图片功能


@RunWith(SpringRunner.class)
@SpringBootTest(classes = TianshunApplication.class)
public class FdfsTest {
 
    @Autowired
    private FastdfsClientUtil fastdfsClientUtil;
 
 
    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("D:\\123.jpg");
 
        System.out.println(fastdfsClientUtil.upload(file));
    }
}

可以看到后台会输出图片的路径

访问地址

http://192.168.111.130:8888/group1/M00/00/00/wKhvglu4_TqAadEtAABs63TOJBQ567.jpg

图片显示出来。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值