比较常用的两种:
导出图片压缩文件,导出excel压缩文件;
导出思路:需要导出的文件转存为byte数组保存到Map,然后遍历压缩成zip
需要引入jar:
一、导出图片压缩文件org.apache.commons commons-compress 1.12
public static byte[] imgageToByte(String imageUrl, String imageName) throws IOException {
File imageFile = new File(imageUrl + File.separator + imageName);
if(!imageFile.exists()){
log.error("---imgageToByte--imageUrl([{}])--is--null", imageUrl);
return null;
}
// return Files.readAllBytes(imageFile.toPath()); // 不推荐
FileInputStream fis = new FileInputStream(imageFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
byte[] imagetByte = bos.toByteArray();
fis.close();
bos.close();
return imagetByte;
}
public static void fileTozip(HttpServletResponse response, String zipName, Map zipMap) {
try{
response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
ServletOutputStream outputStream = response.getOutputStream();
ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream);
zipOutputStream.setUseZip64(Zip64Mode.AsNeeded);
for(String fileName: zipMap.keySet()){
ArchiveEntry archiveEntry = new ZipArchiveEntry(fileName);
zipOutputStream.putArchiveEntry(archiveEntry);
byte[] fileByte = zipMap.get(fileName);
zipOutputStream.write(fileByte);
zipOutputStream.closeArchiveEntry();
}
zipOutputStream.close();
outputStream.close();
}catch (Exception e){
log.error("fileTozip error:[{}]", ExceptionUtils.getStackTrace(e));
}
}
二、导出excel压缩文件
把csv文件转为byte数组,然后再通过fileTozip方法输出zip文件即可
public static byte[] writeCsvAfterToBytes(String[] headers,List



