相關(guān)鏈接:FastDFS安裝教程1(安裝、配置、部署、防盜鏈、nginx、圖片壓縮)
1、下載
git clone https://gitee.com/xiaxia_01/fastdfs-spring-boot-starter.git
cd fastdfs-spring-boot-starter
2、安裝到本地倉庫
mvn clean install
mvn source:jar install
mvn javadoc:jar install
如果直接在maven倉庫相應(yīng)位置復(fù)制該項目生成的jar包,即可忽略以上兩步.
3、添加到項目
<dependency>
<groupId>com.bluemiaomiao</groupId>
<artifactId>fastdfs-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
4、在主配置類上添加注解 (@EnableFastdfsClient)
@EnableFastdfsClient
@SpringBootApplication
public class DemoApplication {
@Autowired
private FastdfsClientService fastdfsClientService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5、添加配置條目(application.yml)
fastdfs:
charset: UTF-8
connect-timeout: 5
http-secret-key: FastDFSFrobot@123.
network-timeout: 30
http-anti-steal-token: true
http-tracker-http-port: 8888
connection-pool-max-idle: 20
connection-pool-max-total: 20
connection-pool-min-idle: 2
nginx-servers: 10.18.25.163:8888
tracker-servers: 10.18.25.163:22122
6、完成以上配置,即可在項目里直接調(diào)用FastdfsClientService使用,示例如下
@Autowired
private FastdfsClientService remoteService;
// 上傳文件
String[] remoteInfo;
try {
remoteInfo = remoteService.autoUpload(image.getBytes(), type);
log.info("上傳的服務(wù)器分組: " + remoteInfo[0]);
log.info("上傳的服務(wù)器ID: " + remoteInfo[1]);
log.info("上傳的服務(wù)器分組和ID: " + remoteInfo[2]);
} catch (Exception e) {
log.error("Upload file error: " + e.getMessage());
return HttpStatus.INTERNAL_SERVER_ERROR;
}
例:

image.png
// 下載文件1
String groupName = "group1";
String remoteFileName = "M00/00/00/wKgrv15nCj2APtknAAFBAKhNrcY954.png";
String remoteFile = "Get file error.";
try {
//當(dāng)啟用防盜鏈機(jī)制時,需要使用該方法下載文件
remoteFile1 = fastdfs.autoDownloadWithToken(groupName, remoteFileName, remoteAddress);
// 當(dāng)沒有啟用防盜鏈機(jī)制時,需要使用該方法下載文件
remoteFile2 = fastdfs.autoDownloadWithoutToken(groupName, remoteFileName, remoteAddress);
} catch (Exception e) {
log.error("Get file error: " + e.getMessage());
}
例:
remoteFile1= http://192.168.43.191:80/group1/M00/00/00/wKgrv15onbqAQ5u3AAFBAKhNrcY705.png?token=7445c459a8a9e5c7962debf7ce1b1af5&ts=1583914426
remoteFile1= http://192.168.43.191:80/group1/M00/00/00/wKgrv15onbqAQ5u3AAFBAKhNrcY705.png
// 下載文件2
String groupName = "group1";
String remoteFileName = "M00/00/00/wKgrv15nCj2APtknAAFBAKhNrcY954.png";
String localFileName = "yy.png";
try {
byte[] bitys = remoteService.download(groupName, remoteFileName);
InputStream ins = new ByteArrayInputStream(bitys);
String contentType = request.getServletContext().getMimeType(localFileName);
String contentDisposition = "attachment;filename=" + localFileName;
// 設(shè)置頭
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", contentDisposition);
// 獲取綁定了客戶端的流
ServletOutputStream output = response.getOutputStream();
// 把輸入流中的數(shù)據(jù)寫入到輸出流中
IOUtils.copy(ins, output);
ins.close();
output.close();
} catch (Exception e) {
log.error("Get file error: " + e.getMessage());
}
例:
說明:一般文件服務(wù)器是不對外開放的,所以這里展示了通過流方式將圖片傳給前端場景,避免因直接訪問導(dǎo)致圖片請求失敗問題

image.png
// 刪除文件
String group = "group1";
String storage = "M00/00/00/wKgrv15mOZ2ARgsCAAFBAKhNrcY006.png";
try {
//i = 0 表示刪除成功
int i = remoteService.delete(group,storage);
} catch (Exception e) {
e.getMessage();
}
例:

image.png
文章參考:
https://gitee.com/bluemiaomiao/fastdfs-spring-boot-starter