目錄
- 5.4 介面開發-根據id洗掉附件
- 5.4.1 介面檔案
- 5.4.2 代碼實作
- 5.4.3 介面測驗
- 5.4.4 測驗ALI和FAST_DFS以及MINIO上傳和洗掉的介面
- 5.4.4.1 阿里云OSS上傳和洗掉
- 5.4.4.2 FastDFS上傳和洗掉
- 5.4.4.3 Minio上傳和洗掉
- 5.5 介面開發-根據業務型別/業務id洗掉附件
- 5.5.1 介面檔案
- 5.5.2 代碼實作
- 5.5.3 介面測驗
5.4 介面開發-根據id洗掉附件
第2-1-2章 傳統方式安裝FastDFS-附FastDFS常用命令
第2-1-3章 docker-compose安裝FastDFS,實作檔案存盤服務
第2-1-5章 docker安裝MinIO實作檔案存盤服務-springboot整合minio-minio全網最全的資料
全套代碼及資料全部完整提供,點此處下載
5.4.1 介面檔案
根據id洗掉附件介面要完成的操作主要有兩個:
- 將客戶端上傳的檔案從指定存盤位置(具體存盤位置由組態檔配置的存盤策略確定)洗掉
- 將檔案資訊從資料庫的pd_attachment表中洗掉
根據id洗掉附件功能的介面檔案如下:

5.4.2 代碼實作
第一步:在AttachmentController中提供檔案洗掉的方法
@ApiOperation(value = "https://www.cnblogs.com/gitBook/p/洗掉檔案", notes = "洗掉檔案")
@ApiImplicitParams({
@ApiImplicitParam(name = "ids[]", value = "https://www.cnblogs.com/gitBook/p/檔案ids", dataType = "array", paramType = "query"),
})
@DeleteMapping
public R<Boolean> remove(@RequestParam(value = "https://www.cnblogs.com/gitBook/p/ids[]") Long[] ids) {
attachmentService.remove(ids);
return success(true);
}
第二步:在AttachmentService介面中擴展remove方法
/**
* 洗掉附件
*
* @param ids
*/
void remove(Long[] ids);
第三步:在AttachmentServiceImpl實作類中實作remove方法
/**
*根據id洗掉附件
* @param ids
*/
@Override
public void remove(Long[] ids) {
if (ArrayUtils.isEmpty(ids)) {
return;
}
//查詢資料庫
List<Attachment> list = super.list(Wrappers.<Attachment>lambdaQuery().
in(Attachment::getId, ids));
if (list.isEmpty()) {
return;
}
//洗掉資料庫中的記錄
super.removeByIds(Arrays.asList(ids));
//物件格式處理
List<FileDeleteDO> fileDeleteDOList =
list.stream().map((fi) -> FileDeleteDO.builder()
.relativePath(fi.getRelativePath()) //檔案在服務器的相對路徑
.fileName(fi.getFilename()) //唯一檔案名
.group(fi.getGroup()) //fastDFS回傳的組 用于FastDFS
.path(fi.getPath()) //fastdfs 的路徑
.build())
.collect(Collectors.toList());
//洗掉檔案
fileStrategy.delete(fileDeleteDOList);
}
5.4.3 介面測驗
第一步:啟動Nacos配置中心
第二步:啟動Nginx服務
第三步:啟動檔案服務
第四步:訪問介面檔案,地址為http://localhost:8765/doc.html

可以看到pd_attachment表中對應的記錄已經洗掉掉了,對應的檔案也已經被洗掉掉了,
5.4.4 測驗ALI和FAST_DFS以及MINIO上傳和洗掉的介面
注:可以修改Nacos中的pd-file-server.yml組態檔,將存盤策略改為ALI和FAST_DFS以及MINIO來測驗檔案的存盤策略是否發生了變化,
5.4.4.1 阿里云OSS上傳和洗掉
- 上傳

- 洗掉

- 洗掉后就資源就無法訪問了

5.4.4.2 FastDFS上傳和洗掉
-
上傳

-
查看資源

-
洗掉操作后再查看

5.4.4.3 Minio上傳和洗掉
- 上傳

- 查看minio中資源存盤情況

- 查看圖片

- 洗掉圖片

- 洗掉成功,資源已經不存在

5.5 介面開發-根據業務型別/業務id洗掉附件
5.5.1 介面檔案
根據業務型別/業務id洗掉附件介面要完成的操作主要有兩個:
- 將客戶端上傳的檔案從指定存盤位置(具體存盤位置由組態檔配置的存盤策略確定)洗掉
- 將檔案資訊從資料庫的pd_attachment表中洗掉
根據業務型別/業務id洗掉附件功能的介面檔案如下:


5.5.2 代碼實作
第一步:在AttachmentController中提供根據業務型別/業務id洗掉檔案的方法
@ApiOperation(value = "https://www.cnblogs.com/gitBook/p/根據業務型別或業務id洗掉檔案",
notes = "根據業務型別或業務id洗掉檔案")
@DeleteMapping(value = "https://www.cnblogs.com/biz")
public R<Boolean> removeByBizIdAndBizType(
@RequestBody
AttachmentRemoveDTO dto) {
attachmentService.removeByBizIdAndBizType(dto.getBizId(),
dto.getBizType());
return success(true);
}
第二步:在AttachmentService介面中擴展removeByBizIdAndBizType方法
/**
* 根據業務id/業務型別洗掉附件
*
* @param bizId
* @param bizType
*/
void removeByBizIdAndBizType(String bizId, String bizType);
第三步:在AttachmentServiceImpl實作類中實作removeByBizIdAndBizType方法
/**
* 根據業務id和業務型別洗掉附件
*
* @param bizId
* @param bizType
*/
@Override
public void removeByBizIdAndBizType(String bizId, String bizType) {
//根據業務類和業務id查詢資料庫
List<Attachment> list = super.list(
Wraps.<Attachment>lbQ()
.eq(Attachment::getBizId, bizId)
.eq(Attachment::getBizType, bizType));
if (list.isEmpty()) {
return;
}
//根據id洗掉檔案
remove(list.stream().mapToLong(
Attachment::getId).boxed().toArray(Long[]::new));
}
5.5.3 介面測驗
第一步:啟動Nacos配置中心
第二步:啟動Nginx服務
第三步:啟動檔案服務
第四步:訪問介面檔案,地址為http://localhost:8765/doc.html

可以看到pd_attachment表中對應的記錄已經洗掉掉了,對應的檔案也已經被洗掉掉了,
第2-1-2章 傳統方式安裝FastDFS-附FastDFS常用命令
第2-1-3章 docker-compose安裝FastDFS,實作檔案存盤服務
第2-1-5章 docker安裝MinIO實作檔案存盤服務-springboot整合minio-minio全網最全的資料
全套代碼及資料全部完整提供,點此處下載
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535977.html
標籤:Java
