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如何使用Jetty實(shí)現(xiàn)嵌入式的Servlet容器
這篇文章主要介紹了Java使用Jetty實(shí)現(xiàn)嵌入式的Servlet容器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來(lái)一起了解一下吧2019-06-06
Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Java實(shí)現(xiàn)遞歸讀取文件夾下的所有文件
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)遞歸讀取文件夾下的所有文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
手動(dòng)部署java項(xiàng)目到k8s中的實(shí)現(xiàn)
本文主要介紹了手動(dòng)部署java項(xiàng)目到k8s中的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
java poi sax方式處理大數(shù)據(jù)量excel文件
這篇文章主要介紹了java poi sax方式處理大數(shù)據(jù)量excel文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

