1. 支持多张图片下载
/**
* 下载附件zip
*/
@PostMapping("downloadZip")
public void downloadZip(@RequestBody ShipmentAnnexVo shipmentAnnexVo, HttpServletRequest request, HttpServletResponse response) {
shipmentAnnexService.downloadZip(shipmentAnnexVo, request, response);
}
2. serviceImpl实现逻辑
// zip文件名标签:zipName,JAVA,zip,annex,压缩,new,response,out From: https://www.cnblogs.com/df-blog/p/17350362.html
String zipName = DateUtil.getNowDateYMDHMS();
try {
// 创建zip输出流,输出到客户端
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
zipName = URLEncoder.encode(zipName, "UTF-8");
response.reset();
response.setCharacterEncoding("UTF-8");
// 不同类型的文件对应不同的MIME类型
response.setContentType("application/octet-stream");
// inline在浏览器中直接显示,不提示用户下载
// attachment弹出对话框,提示用户进行下载保存本地
// 默认为inline方式
response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
for (ShipmentAnnex annex : annexList) {
InputStream in = tencentMinioManage.get(bucketName, annex.getAnnexFileName());
String fileName = annex.getOriginFileName().contains(".") ? annex.getOriginFileName() : annex.getOriginFileName() + ".jpg";
out.putNextEntry(new ZipEntry(fileName));
int len;
byte[] buf = new byte[4096];
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (Exception e) {
throw new BusinessException("下载ZIP异常!" + e.getMessage());
}