//请求方法加 HttpServletResponse response
@GetMapping("/download/plan")
public AjaxResult downLoadPlan( HttpServletResponse response) throws Exception {
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
List<String> pics2 = Arrays.asList("图片地址1","图片地址2");
//循环得到图片地址
for (int i = 0; i < pics2.size(); i++) {
String img = jsonObject.pics2.get(i);
//得到下载链接
String imgLinks = img.replaceAll("//", "");
URL url3 = new URL("http://" + imgLinks);
//得到图片的流
InputStream stream = URLUtil.getStream(url3);
FastByteArrayOutputStream read = IoUtil.read(stream);
byte[] bytes = read.toByteArray();
try {
// 文件流添加到zip流
zip.putNextEntry(new ZipEntry(FileUtil.getName(imgLinks)));
zip.write(bytes);
IOUtils.closeQuietly(read);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
log.error("下载失败!", e);
}
}
IOUtils.closeQuietly(zip);
byte[] bytes = outputStream.toByteArray();
//由响应response直接输入到前端
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("1.zip", "UTF-8"));
response.addHeader("Content-Length", "" + bytes.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(bytes, response.getOutputStream());