public JsonResult<DrugReportFile> drugreportfilegeneratezip (HttpServletResponse response, int history_id , List store) throws IOException {
String zipname = "首营品种质量文档.rar";
InputStream inputStream = null;
ZipOutputStream zos = null;
System.out.println("store:" + store.toString());
//数组去重
Set set = new HashSet();
List newList = new ArrayList();
set.addAll(store);
newList.addAll(set);
System.out.println("newlistsize:" + newList.size());
//生成压缩文件前先搜索客户地点下的所有药检文件
for (int i = 0; i < newList.size(); i++) {
String new_store = (String) newList.get(i);
System.out.println("第" + i + "个store:" + new_store);
List<DrugReportFile> medicalfilelist = drugReportFileMapper.drugreportfilelist_file(history_id, new_store);
System.out.println("medicalfilelist:" + medicalfilelist);
List<String> filenamelist = medicalfilelist.stream().filter((e) -> e.getFilename() != null).map(DrugReportFile::getFilename).collect(Collectors.toList());
System.out.println("filenamelist:" + filenamelist.toString());
//遍历需要批量下载的文件
for (int j = 0; j < filenamelist.size(); j++) {
System.out.println("filename:" + filenamelist.get(j));
//获取文件名
String filename = filenamelist.get(j);
//定义下载文件的路径
String filepath = "E://nginx-1.12.2//nginx-1.12.2//temp//" + filename;
try {
//修改响应的头部属性content-disposition值为attachment,使下载的文件可以另存为,并且解决了下载文件名的乱码
response.setHeader("content-disposition", "attachment;filename=" + zipname + ";filename*=UTF-8''"
+ URLEncoder.encode(filename, "UTF-8"));
//要下载的文件文件夹
zos = new ZipOutputStream(response.getOutputStream());
File file_io = new File(filepath);
System.out.println("文件:" + new_store + "/" + filenamelist.get(j));
//创建zip格式的文件
zos.putNextEntry(new ZipEntry(new_store + "/" + filename));
//写入文件流
inputStream = new FileInputStream(file_io);
System.out.println("inputStream:" + inputStream);
byte buffer[] = new byte[4096];
int temp = 0;
//filename參数指定下载的zip文件名称
while ((temp = inputStream.read(buffer)) > 0) {
zos.write(buffer,0,temp);
}
System.out.println("OK");
/*zos.flush();*/
zos.closeEntry();
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}

springboot压缩文件用winrar解压报错文件已损坏
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 张张小 2020-12-04 20:13关注
response.getOutputStream() 这个不能多次调用,你代码执行应该会报错的
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报