您好,登錄后才能下訂單哦!
這篇文章主要介紹了springboot2.0如何通過fastdfs實(shí)現(xiàn)文件分布式上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1. 引入依賴
在父工程中,我們已經(jīng)管理了依賴,版本為:
<fastDFS.client.version>1.26.7</fastDFS.client.version>
因此,這里我們直接在工程的pom.xml中引入坐標(biāo)即可:
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> </dependency>
@Configuration @Import(FdfsClientConfig.class) // 解決jmx重復(fù)注冊(cè)bean的問題 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
2. 在application.yml文件中編寫FastDFS屬性
fdfs: so-timeout: 1501 # 超時(shí)時(shí)間 connect-timeout: 601 # 連接超時(shí)時(shí)間 thumb-image: # 縮略圖 width: 60 height: 60 tracker-list: # tracker地址:你的虛擬機(jī)服務(wù)器地址+端口(默認(rèn)是22122) - 192.168.0.22:22122
3. 測(cè)試
package com.leyou.upload.test; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; /** * @author john * @date 2019/12/6 - 15:09 */ @SpringBootTest @RunWith(SpringRunner.class) public class FastDFSTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { // 要上傳的文件 File file = new File("D:\\imooc\\project\\images\\1.jpg"); // 上傳并保存圖片,參數(shù):1-上傳的文件流 2-文件的大小 3-文件的后綴 4-可以不管他 StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "jpg", null); // 帶分組的路徑 System.out.println(storePath.getFullPath()); // 不帶分組的路徑 System.out.println(storePath.getPath()); } @Test public void testUploadAndCreateThumb() throws FileNotFoundException { File file = new File("D:\\imooc\\project\\images\\2.jpg"); // 上傳并且生成縮略圖 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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。