serviceImpl代码
@Override
public void QRCodePage(MPage page, TbJmzzxqDto param) throws IOException, WriterException {
String fullDirectory = filesPath + "/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime()).toString();
System.out.println(fullDirectory);
File dir = new File(fullDirectory);
if (!dir.exists()) {// 判断目录是否存在
dir.mkdir();
}
//得到数据
MPage<TbJmzzxqDto> mPage = this.residentialPageList(page, param);
List<TbJmzzxqDto> dataList = mPage.getRecords();
//循环生成二维码图片
for (TbJmzzxqDto jmzzxqDto : dataList) {
//二维码内容
String content = "tb_jmzzxq," + jmzzxqDto.getId();
//生成二维码
QRCodeUtil.generateQRCodeImage(content, fullDirectory + "/" + jmzzxqDto.getId() + ".png");
}
ZipUtil zca = new ZipUtil(filesPath+"\\zzxq.zip");
zca.compressExe(fullDirectory);
}
ZipUtil代码:
private File zipFile;
/**
* 压缩文件构造函数
* @param finalFile 最终压缩生成的压缩文件:目录+压缩文件名.zip
*/
public ZipUtil(String finalFile) {
zipFile = new File(finalFile);
}
/**
* 执行压缩操作
* @param srcPathName 需要被压缩的文件/文件夹
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName);
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
zip.addFileset(fileSet);
zip.execute();
}
压缩成功也可以解压
Controller代码:
@ApiOperation(value = "下载zip", notes = "下载zip")
@GetMapping("/v1/QRCodePage")
public void QRCodePage(MPage page, TbJmzzxqDto param, HttpServletResponse response) throws Exception {
tbJmzzxqService.QRCodePage(page, param);
byte[] buffer = null;
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=住宅小区.zip");
ServletOutputStream os = response.getOutputStream();
File file = new File(filesPath + "\\zzxq.zip");
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
os.write(buffer);
os.flush();
os.close();
}
vue代码:
handleTableQr(){
let page={
current:this.current,
size:this.size
}
let param = Object.assign(this.queryInfo, page);
this.DZGET(this.APIURL.ZZXQ_QR, param).then((res) => {
const link = document.createElement("a");
let blob = new Blob([res]);
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.setAttribute("download", "住宅小区.zip");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}
下载成功,解压失败
求知道的人指教是什么问题