@RequestMapping("/zipDownload2")
public void zipDownload2(HttpServletResponse resp) throws baseAppException {
//要下载的文件
List fileUrlList = new ArrayList<>();
fileUrlList.add("D:\笔记文档\linux下搭建项目服务.txt");
fileUrlList.add("C:\Users\Downloads\客户拜访报告模板.xlsx");
resp.setHeader("Content-disposition", "attachment;filename=压缩包.zip");
//最终zip文件
try (ZipOutputStream zipOut=new ZipOutputStream(resp.getOutputStream())){
// 循环调用压缩文件方法,将一个一个需要下载的文件打入压缩文件包
for(String fileUrl : fileUrlList){
InputStream inputStream = new FileInputStream(fileUrl);
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(inputStream);
zipOut.putNextEntry(new ZipEntry(FileUtils.getFileName(Utils.decodeUrl(fileUrl))));
zipOut.write(bytes);
}
} catch (Exception e) {
throw new baseAppException("zip download failed."+e.getMessage(), e);
}
}