FastDFS整合SpringBoot
搭建好fastDFS後當然就是要開始整合到項目中使用,這裡使用FastDFS-Client和SpringBoot整合。
如果檢視分布式檔案系統原理和伺服器搭建,可以點選檢視。
工具
jdk | SpringBoot | FastDFS-Client | maven | FastDFS |
---|---|---|---|---|
11 | 2.2.2 | 1.27.1 | 3.6.3 | 6.06 |
FastDFS-Client
介紹:
在原作者YuQing與yuqih釋出的java用戶端基礎上進行了大量重構工作,便于Java工作者學習與閱讀。
主要特性:
- 對關鍵部分代碼加入了單元測試,便于了解與服務端的接口交易,提高接口品質
- 将以前對byte硬解析風格重構為使用 對象+注解 的形式,盡量增強了代碼的可讀性
- 支援對服務端的連接配接池管理(commons-pool2)
- 支援上傳圖檔時候檢查圖檔格式,并且自動生成縮略圖
- 在SpringBoot當中自動導入依賴
其他的可以看github介紹FastDFS-Client
整合開始
建立SpringBoot項目
依賴包:
<!--fastdfs-client-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
jdk11可能會缺失一些包,需要導入Commons裡包。但是jdk’8應該不用。
配置檔案:
server:
port: 8888
### fdfs配置
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image:
width: 150
height: 150
tracker-list:
# 可寫多個tracker-server位址
- 192.168.xxx.xxx:22122
Application編輯
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
@SpringBootApplication
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastdfsFileApplication {
public static void main(String[] args) {
SpringApplication.run(FastdfsFileApplication.class, args);
}
}
單元測試:
@SpringBootTest
class FastdfsFileApplicationTests {
private final static Logger log = LoggerFactory.getLogger(FastdfsFileApplicationTests.class);
@Resource
private FastFileStorageClient fastFileStorageClient;
@Resource
private ThumbImageConfig thumbImageConfig;
/**
* 上傳測試
*/
@Test
void testUpload() throws FileNotFoundException {
var path = "圖檔的路徑";
var image = new File(path);
// 上傳圖檔
var fastJpg = fastFileStorageClient.uploadFile(new FileInputStream(image),
image.length(), "jpg", null);
// 帶分組的路徑
log.info(fastJpg.getFullPath());
// 不帶分組的路徑
log.warn(fastJpg.getPath());
}
/**
* 測試删除相片
*/
@Test
void delImag(){
fastFileStorageClient.deleteFile("http://192.168.247.201/group1/M00/00/00/wKj3yV4u5cCAarJ1AA2CBRqtFgE245.jpg");
log.warn("ok");
}
/**
* 測試上傳縮略圖
*/
@Test
void testThumbImage() throws FileNotFoundException {
var path = "E:\\idea\\ServiceCode\\fastdfs-file\\src\\main\\resources\\static\\img\\upload.jpg";
var image = new File(path);
var thumbImage = fastFileStorageClient.uploadImageAndCrtThumbImage(new FileInputStream(image), image.length(), "jpg", null);
log.info("縮略圖路徑:" + thumbImageConfig.getThumbImagePath(thumbImage.getPath()));
log.warn("圖檔全路徑:" + thumbImage.getFullPath());
log.debug("圖檔分組:" + thumbImage.getGroup());
log.error("列印資訊:" + thumbImage.toString());
}
/**
* 檔案資訊
*/
@Test
public void testFileInfo(){
String uri = "http://192.168.247.201/group1/M00/00/00/wKj3yV4xQOSAPoyQAAzn60z3Hss404.jpg";
var filePath = uri.substring(uri.indexOf("group"));
var group = filePath.substring(0, filePath.indexOf("/"));
var path = filePath.substring(filePath.indexOf("/")+1);
log.warn("group:"+group+"<-->path:"+path);
var fileInfo = fastFileStorageClient.queryFileInfo(group,path);
log.info(fileInfo.toString());
}
}
由此可以自定義工具類
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* @author 佐斯特勒
* <p>
* FastDFS包裝類
* 用來包裝操作FastDFSClient
* </p>
* @version v1.0.0
* @date 2020/1/28 1:14
* @see FastDFSWrapper
**/
@Component
@Slf4j
public class FastDFSWrapper {
@Resource
private FastFileStorageClient fastFileStorageClient;
@Resource
private ThumbImageConfig thumbImageConfig;
/**
* 檔案上傳
* 最後傳回fastDFS中的檔案名稱;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc
*
* @param bytes 檔案位元組
* @param fileSize 檔案大小
* @param extension 檔案擴充名
* @return fastDfs路徑
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
var byteArrayInputStream = new ByteArrayInputStream(bytes);
var storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
log.info(storePath.getGroup() + "==" + storePath.getPath() + "======" + storePath.getFullPath());
return storePath.getFullPath();
}
/**
* 檔案上傳 原圖+150x150縮略圖
* @param bytes 檔案位元組
* @param fileSize 檔案大小
* @param extension 檔案擴充名
* @return fastDfs路徑
*/
public String[] uploadThumbFile(byte[] bytes, long fileSize, String extension) {
var byteArrayInputStream = new ByteArrayInputStream(bytes);
var storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(byteArrayInputStream, fileSize, extension, null);
log.info(storePath.getGroup() + "==" + thumbImageConfig.getThumbImagePath(storePath.getPath()) + "======" + storePath.getFullPath());
return new String[]{thumbImageConfig.getThumbImagePath(storePath.getFullPath()), storePath.getFullPath()};
}
/**
* 下載下傳檔案
* 傳回檔案位元組流大小
* @param fileUrl 檔案URL
* @return 檔案位元組
* @throws IOException .
*/
public byte[] downloadFile(String fileUrl) throws IOException {
fileUrl = fileUrl.substring(fileUrl.indexOf("group"));
var group = fileUrl.substring(0, fileUrl.indexOf("/"));
var path = fileUrl.substring(fileUrl.indexOf("/") + 1);
var downloadByteArray = new DownloadByteArray();
log.info("group:"+group+"--path:"+path);
return fastFileStorageClient.downloadFile(group, path, downloadByteArray);
}
/**
* 删除圖檔
* @param fileUrl 檔案位址
*/
public void delFile(String fileUrl){
fastFileStorageClient.deleteFile(fileUrl);
}
}
大家可以參照上面的工具類來制作檔案處理。更多的案例可以看FastDFS-Client作者的
test/service
。FastDFS-Client源碼
下面是我做的下案例
有上傳圖檔和上傳圖檔顯示原圖與縮略圖的功能,還有下載下傳圖檔功能
案例位址:gitee位址
覺得好别忘了點個贊(✪ω✪)(✪ω✪)