FastDFS與springboot整合例子


余慶先生提供了一個Java客戶端,但是作為一個C程序員,寫的java代碼可想而知。而且已經很久不維護了。

這里推薦一個開源的FastDFS客戶端,支持最新的SpringBoot2.0。

配置使用極為簡單,支持連接池,支持自動生成縮略圖,狂拽酷炫吊炸天啊,有木有。

地址:tobato/FastDFS_client

引入依賴

在父工程中,我們已經管理了依賴,版本為:

<fastDFS.client.version>1.25.2-RELEASE</fastDFS.client.version>

因此,這里我們直接引入坐標即可:

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

引入配置類

純java配置:

@Configuration
@Import(FdfsClientConfig.class)
// 解決jmx重復注冊bean的問題
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

編寫FastDFS屬性

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image: # 縮略圖
    width: 60
    height: 60
  tracker-list: # tracker地址
    - 192.168.56.101:22122

測試

@RunWith(SpringRunner.class)
@SpringBootTest(classes = LyUploadService.class)
public class FdfsTest {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Test
    public void testUpload() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上傳並且生成縮略圖
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "png", null);
        // 帶分組的路徑
        System.out.println(storePath.getFullPath());
        // 不帶分組的路徑
        System.out.println(storePath.getPath());
    }

    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("D:\\test\\baby.png");
        // 上傳並且生成縮略圖
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);
        // 帶分組的路徑
        System.out.println(storePath.getFullPath());
        // 不帶分組的路徑
        System.out.println(storePath.getPath());
        // 獲取縮略圖路徑
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

結果:

group1/M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630_60x60.png

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM