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

圖片顯示出來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章