实体类
@Data
public class PackageDownloadDto {
private String name;
private List<String> urlList;
}
controller 层
/**
* 打包下载
* @param
* @return
*/
@ApiOperation(value = "打包下载")
@PostMapping("/getPackageDownload")
public void getPackageDownload(HttpServletResponse response , @RequestBody PackageDownloadDto dto) {
markOverviewService.getPackageDownload(response,dto);
}
service
/**
* 打包下载
*
* @param str
* @return
*/
private String zipFilePath = "E:\\worker"; // 存在本地一个位置
@Override
public void getPackageDownload(HttpServletResponse response, PackageDownloadDto dto) {
List<String> fileUrlList = new ArrayList<>();
dto.getUrlList().forEach(x -> {
if (x.contains(",")) {
String[] split = x.split(",");
fileUrlList.addAll(Arrays.asList(split));
} else {
fileUrlList.add(x);
}
});
//这里我是需要公告名称给压缩包命名的所以获取公告名称。
//获取公告名称,用于给压缩包命名,因为它们一个共用一个公告名所以随便取集合中的一个就可以
// String packageName = fileTempById.get(0).getAbout()+".zip";
String packageName = dto.getName() + ".zip";
//判断集合是否有路径
if (fileUrlList.size() != 0) {
// 压缩输出流,包装流,将临时文件输出流包装成压缩流,将所有文件输出到这里,打成zip包
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath + packageName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 循环调用压缩文件方法,将一个一个需要下载的文件打入压缩文件包
for (String path : fileUrlList) {
//将路径进行拆分,将上面拼接的真实文件名拆分出来作为参数传递进去
int lastIndexOf = path.lastIndexOf("/") + 1;
String realFileName = path.substring(lastIndexOf, path.length());
try {
//调用工具类方法,传递路径和压缩流,压缩包文件的名字
fileToZip(path, zipOut);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
continue;
}
}
try {
// 压缩完成后,关闭压缩流
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
//设置内容内容型应用下载,设置字符集
response.setContentType("application/x-download;charset=ISO8859-1");
//告诉客户端该文件不是直接解析而是以附件的形式打开(下载)
response.setHeader("Content-Disposition", "attachment;filename=" + packageName);
//提高作用域
ServletOutputStream outputStream = null;
FileInputStream inputStream = null;
try {
//该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
outputStream = response.getOutputStream();
inputStream = new FileInputStream(zipFilePath + packageName);
} catch (IOException e) {
e.printStackTrace();
}
try {
//文件下载,复制
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 关闭输入流
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//下载完成,删掉zip包
File fileTempZip = new File(zipFilePath + packageName);
fileTempZip.delete();
}
}
public static void fileToZip(String filePath, ZipOutputStream zipOut) throws IOException {
// 需要压缩的文件
MinioUtils minioUtils = new MinioUtils();
//创建文件输入流
String[] splitFileUrl = URLUtil.decode(filePath).split("/");
String name = splitFileUrl[splitFileUrl.length - 1];
InputStream inputStream = minioUtils.getObjectStream(name);
byte[] bufferArea = new byte[1024 * 10];
BufferedInputStream bufferStream = new BufferedInputStream(inputStream, 1024 * 10);
// 将当前文件作为一个zip实体写入压缩流,realFileName代表压缩文件中的文件名称
zipOut.putNextEntry(new ZipEntry(name));
int length = 0;
// 写操作
while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
zipOut.write(bufferArea, 0, length);
}
//关闭流
// 需要注意的是缓冲流必须要关闭流,否则输出无效
bufferStream.close();
// 压缩流不必关闭,使用完后再关
}