feat: 压缩包下载接口
This commit is contained in:
+83
@@ -1,5 +1,6 @@
|
||||
package com.jero.modules.system.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
@@ -33,6 +34,14 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户表 前端控制器
|
||||
@@ -277,6 +286,80 @@ public class CommonController {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览图片&下载文件
|
||||
*
|
||||
* @param map 传入文件ids
|
||||
* @param response HttpServletResponse
|
||||
*/
|
||||
@PostMapping(value = "/zipDownload")
|
||||
public void zipDownload(@RequestBody Map<String, String> map, HttpServletResponse response) {
|
||||
String ids = map.get("ids");
|
||||
if (StrUtil.isBlank(ids)) {
|
||||
throw new JeroBootException("ids不能为空");
|
||||
}
|
||||
List<String> idList = Arrays.asList(ids.split(","));
|
||||
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.in(OSSFile::getId, idList);
|
||||
List<OSSFile> ossFileList = ossFileService.list(queryWrapper);
|
||||
if (ossFileList == null || ossFileList.isEmpty() || ossFileList.size() != idList.size()) {
|
||||
throw new JeroBootException("实际存在文件数量与预期数量不匹配");
|
||||
}
|
||||
|
||||
response.setContentType("application/zip");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.addHeader("Content-Disposition", "attachment; filename=\"files.zip\"");
|
||||
|
||||
try (ZipOutputStream zippedOut = new ZipOutputStream(response.getOutputStream())) {
|
||||
for (OSSFile ossFile : ossFileList) {
|
||||
String fileUrl = ossFile.getUrl();
|
||||
String fileName = ossFile.getFileName(); // Assume fileName is set correctly in OSSFile
|
||||
InputStream inputStream = null;
|
||||
|
||||
if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
|
||||
String filePath = uploadpath + File.separator + fileUrl;
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new RuntimeException("文件不存在: " + filePath);
|
||||
}
|
||||
inputStream = new BufferedInputStream(Files.newInputStream(Paths.get(filePath)));
|
||||
} else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
|
||||
String minioUrl = MinioUtil.getMinioUrl() + MinioUtil.getBucketName() + "/";
|
||||
String url = fileUrl.replace(minioUrl, "");
|
||||
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
||||
}
|
||||
|
||||
// Add ZIP entry to output stream.
|
||||
zippedOut.putNextEntry(new ZipEntry(fileName));
|
||||
|
||||
// Transfer bytes from the file to the ZIP file
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
zippedOut.write(buf, 0, len);
|
||||
}
|
||||
|
||||
// Complete the entry
|
||||
zippedOut.closeEntry();
|
||||
inputStream.close();
|
||||
|
||||
Integer downloadNumber = ossFile.getDownloadNumber();
|
||||
if (downloadNumber == null) {
|
||||
ossFile.setDownloadNumber(0);
|
||||
} else {
|
||||
ossFile.setDownloadNumber(downloadNumber + 1);
|
||||
}
|
||||
ossFileService.saveDownloadTimes(ossFile.getId(), null);
|
||||
ossFileService.updateAndPush(ossFile);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("文件打包失败" + e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 预览图片&下载文件
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user