目錄
- 5.6 介面開發-根據檔案id打包下載附件
- 5.6.1 介面檔案
- 5.6.2 代碼實作
- 5.6.3 介面測驗
- 5.7 介面開發-根據業務型別/業務id打包下載
- 5.7.1 介面檔案
- 5.7.2 代碼實作
- 5.7.3 介面測驗
5.6 介面開發-根據檔案id打包下載附件
第2-1-2章 傳統方式安裝FastDFS-附FastDFS常用命令
第2-1-3章 docker-compose安裝FastDFS,實作檔案存盤服務
第2-1-5章 docker安裝MinIO實作檔案存盤服務-springboot整合minio-minio全網最全的資料
全套代碼及資料全部完整提供,點此處下載
5.6.1 介面檔案
根據檔案id打包下載附件介面分兩種情況進行下載:
1、如果客戶端提交的檔案id只有一個,則下載對應的原始檔案
2、如果客戶端提交的檔案id有多個,則將對應的多個原始檔案進行壓縮,最終下載的是壓縮后的檔案
介面檔案如下:

5.6.2 代碼實作
第一步:在AttachmentController中提供根據檔案id打包下載檔案的方法
/**
* 下載一個檔案或多個檔案打包下載
*
* @param ids 檔案id
* @param response
* @throws Exception
*/
@ApiOperation(value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/根據檔案id打包下載", notes = "根據附件id下載多個打包的附件")
@GetMapping(value = "https://www.cnblogs.com/download", produces = "application/octet-stream")
public void download(
@ApiParam(name = "ids[]", value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/檔案id 陣列")
@RequestParam(value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/ids[]") Long[] ids,
HttpServletRequest request, HttpServletResponse response) throws Exception {
BizAssert.isTrue(ArrayUtils.isNotEmpty(ids),
BASE_VALID_PARAM.build("附件id不能為空"));
//根據檔案id下載檔案
attachmentService.download(request, response, ids);
}
第二步:在AttachmentService介面中擴展download方法
/**
* 根據檔案id下載附件
*
* @param request
* @param response
* @param ids
* @throws Exception
*/
void download(HttpServletRequest request,
HttpServletResponse response,
Long[] ids) throws Exception;
第三步:在AttachmentServiceImpl實作類中實作download方法
@Autowired
private FileBiz fileBiz;
/**
* 根據檔案id下載檔案
* @param request
* @param response
* @param ids
* @throws Exception
*/
@Override
public void download(HttpServletRequest request,
HttpServletResponse response,
Long[] ids) throws Exception {
//根據檔案id查詢資料庫
List<Attachment> list =
(List<Attachment>) super.listByIds(Arrays.asList(ids));
down(request, response, list);
}
/**
* 檔案下載
* @param request
* @param response
* @param list
* @throws Exception
*/
private void down(HttpServletRequest request, HttpServletResponse response,
List<Attachment> list) throws Exception {
if (list.isEmpty()) {
throw BizException.wrap("您下載的檔案不存在");
}
List<FileDO> fileDOList =
list.stream().map((file) ->FileDO.builder()
.url(file.getUrl())
.submittedFileName(file.getSubmittedFileName())
.size(file.getSize())
.dataType(file.getDataType())
.build())
.collect(Collectors.toList());
fileBiz.down(fileDOList, request, response);
}
第四步:創建FileBiz,統一進行檔案下載
package com.itheima.pinda.file.biz;
import cn.hutool.core.util.StrUtil;
import com.itheima.pinda.file.domain.FileDO;
import com.itheima.pinda.file.enumeration.DataType;
import com.itheima.pinda.file.utils.ZipUtils;
import com.itheima.pinda.utils.NumberHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 檔案和附件的一些公共方法
*/
@Component
@Slf4j
public class FileBiz {
/**
* 構建新檔案名稱
* @param filename
* @param order
* @return
*/
private static String buildNewFileName(String filename, Integer order) {
return StrUtil.strBuilder(filename).
insert(filename.lastIndexOf("."), "(" + order + ")").toString();
}
/**
* 下載檔案
* @param list
* @param request
* @param response
* @throws Exception
*/
public void down(List<FileDO> list,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
int fileSize = list.stream().filter(
(file) -> file != null &&
!DataType.DIR.eq(file.getDataType()) &&
StringUtils.isNotEmpty(file.getUrl()))
.mapToInt(
(file) -> NumberHelper.intValueOf0(file.getSize())).sum();
String extName = list.get(0).getSubmittedFileName();
if (list.size() > 1) {
extName = StringUtils.substring(extName, 0,
StringUtils.lastIndexOf(extName, ".")) +
"等.zip";
}
Map<String, String> map = new LinkedHashMap<>(list.size());
Map<String, Integer> duplicateFile = new HashMap<>(list.size());
list.stream()
//過濾不符合要求的檔案
.filter((file) -> file != null && !DataType.DIR.eq(file.getDataType()) && StringUtils.isNotEmpty(file.getUrl()))
//回圈處理相同的檔案名
.forEach((file) -> {
String submittedFileName = file.getSubmittedFileName();
if (map.containsKey(submittedFileName)) {
if (duplicateFile.containsKey(submittedFileName)) {
duplicateFile.put(submittedFileName, duplicateFile.get(submittedFileName) + 1);
} else {
duplicateFile.put(submittedFileName, 1);
}
submittedFileName = buildNewFileName(submittedFileName, duplicateFile.get(submittedFileName));
}
map.put(submittedFileName, file.getUrl());
});
ZipUtils.zipFilesByInputStream(map, Long.valueOf(fileSize), extName, request, response);
}
}
5.6.3 介面測驗
第一步:啟動Nacos配置中心
第二步:啟動Nginx服務
第三步:啟動檔案服務
第四步:訪問介面檔案,地址為http://localhost:8765/doc.html

5.7 介面開發-根據業務型別/業務id打包下載
5.7.1 介面檔案
根據業務型別/業務id打包下載檔案介面分兩種情況進行下載:
1、如果根據業務型別和業務id匹配到的檔案只有一個,則下載對應的原始檔案
2、如果根據業務型別和業務id匹配到的檔案有多個,則將對應的多個原始檔案進行壓縮,最終下載的是壓縮后的檔案
介面檔案如下:

5.7.2 代碼實作
第一步:在AttachmentController中提供根據業務型別和業務id打包下載的方法
/**
* 根據業務型別或者業務id其中之一,或者2個同時打包下載檔案
*
* @param bizIds 業務id
* @param bizTypes 業務型別
*
*/
@ApiImplicitParams({
@ApiImplicitParam(name = "bizIds[]", value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/業務id陣列", dataType = "array", paramType = "query"),
@ApiImplicitParam(name = "bizTypes[]", value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/業務型別陣列", dataType = "array", paramType = "query"),
})
@ApiOperation(value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/根據業務型別/業務id打包下載", notes = "根據業務id下載一個檔案或多個檔案打包下載")
@GetMapping(value = "https://www.cnblogs.com/download/biz", produces = "application/octet-stream")
public void downloadByBiz(
@RequestParam(value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/bizIds[]", required = false) String[] bizIds,
@RequestParam(value = "https://www.cnblogs.com/gitBook/archive/2022/11/19/bizTypes[]", required = false) String[] bizTypes,
HttpServletRequest request, HttpServletResponse response) throws Exception {
BizAssert.isTrue(!(ArrayUtils.isEmpty(bizTypes) && ArrayUtils.isEmpty(bizIds)), BASE_VALID_PARAM.build("附件業務id和業務型別不能同時為空"));
attachmentService.downloadByBiz(request, response, bizTypes, bizIds);
}
第二步:在AttachmentService介面中擴展downloadByBiz方法
/**
* 根據業務id和業務型別下載附件
*
* @param request
* @param response
* @param bizTypes
* @param bizIds
* @throws Exception
*/
void downloadByBiz(HttpServletRequest request, HttpServletResponse response,
String[] bizTypes, String[] bizIds) throws Exception;
第三步:在AttachmentServiceImpl實作類中實作downloadByBiz方法
/**
* 根據業務id和業務型別下載附件
*
* @param request
* @param response
* @param bizTypes
* @param bizIds
* @throws Exception
*/
@Override
public void downloadByBiz(HttpServletRequest request, HttpServletResponse response, String[] bizTypes, String[] bizIds) throws Exception {
List<Attachment> list = super.list(
Wraps.<Attachment>lbQ()
.in(Attachment::getBizType, bizTypes)
.in(Attachment::getBizId, bizIds));
down(request, response, list);
}
5.7.3 介面測驗
第一步:啟動Nacos配置中心
第二步:啟動Nginx服務
第三步:啟動檔案服務
第四步:訪問介面檔案,地址為http://localhost:8765/doc.html

第2-1-2章 傳統方式安裝FastDFS-附FastDFS常用命令
第2-1-3章 docker-compose安裝FastDFS,實作檔案存盤服務
第2-1-5章 docker安裝MinIO實作檔案存盤服務-springboot整合minio-minio全網最全的資料
全套代碼及資料全部完整提供,點此處下載
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/536095.html
標籤:其他
上一篇:day18-web工程路徑
下一篇:第2-3-6章 打包批量下載附件的介面開發-檔案存盤服務系統-nginx/fastDFS/minio/阿里云oss/七牛云oss
