springboot2.0如何通過(guò)fastdfs實(shí)現(xiàn)文件分布式上傳
這篇文章主要介紹了springboot2.0如何通過(guò)fastdfs實(shí)現(xiàn)文件分布式上傳,文中通過(guò)示例代碼介紹的非常詳細(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的問(wèn)題 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
2. 在application.yml文件中編寫(xiě)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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java字符串寫(xiě)入文件三種方式的實(shí)現(xiàn)
這篇文章主要介紹了 Java字符串寫(xiě)入文件三種方式的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解
這篇文章主要介紹了Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解,HashMap的key保留了對(duì)實(shí)際對(duì)象的強(qiáng)引用,這意味著只要HashMap對(duì)象不被銷毀,還HashMap的所有key所引用的對(duì)象就不會(huì)被垃圾回收,需要的朋友可以參考下2023-09-09Springboot-注解-操作日志的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot-注解-操作日志的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
這篇文章主要介紹了SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07java實(shí)現(xiàn)文件編碼轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件編碼轉(zhuǎn)換的方法,分享一個(gè)文件編碼轉(zhuǎn)換的工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05java實(shí)現(xiàn)銀行ATM管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)銀行ATM管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Mybatis-Plus實(shí)體類注解方法與mapper層和service層的CRUD方法
CRUD是指在做計(jì)算處理時(shí)的增加(Create)、讀取查詢(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě)。主要被用在描述軟件系統(tǒng)中DataBase或者持久層的基本操作功能,下面讓我們一起看看吧2022-03-03ReentrantLock 非公平鎖實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了ReentrantLock 非公平鎖實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12