try {
// 添加到zip,设置文件名,后缀.png
zip.putNextEntry(new ZipEntry(String.format("%d.%s.png", i + 1, qrcode.getCode())));
// 查询是否配置了logo,如果有logo,则把logo添加到二维码中
BufferedImage logo = CustomerBrandCache.getLogo(qrcode.getCustomerBrandId());
QrConfig config = new QrConfig();
config.setWidth(width).setHeight(width);
if (logo != null) {
config.setImg(logo);
}
// 生成二维码图片
byte[] bytes = QrCodeUtil.generatePng(qrcode.getLinkUrl(), config);
// 将byte[]写入到压缩包中
IOUtils.write(bytes, zip);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
log.error("addQrcode,error:", e);
}
}
return outputStream.toByteArray();
} catch (Exception e) {
log.error("", e);
}
return new byte[0];
}
/\*\*
* 生成zip文件,设置响应头为文件下载
*/
private void zip(HttpServletResponse response, byte[] data) throws IOException {
response.reset();
response.addHeader(“Access-Control-Allow-Origin”, “*”);
response.addHeader(“Access-Control-Expose-Headers”, “Content-Disposition”);
response.setHeader(“Content-Disposition”, “attachment; filename=“qrcode.zip””);
response.addHeader(“Content-Length”, “” + data.length);
response.setContentType(“application/octet-stream; charset=UTF-8”);
IOUtils.write(data, response.getOutputStream());
}
>
> * 通过`cn.hutool.extra.qrcode.QrCodeUtil`生成二维码图片,得到`byte[]`
> * 通过`java.util.zip.ZipOutputStream`将`byte[]`写入压缩包
> * 通过`java.io.ByteArrayOutputStream`返回完整的`byte[]`
> * 全部写入完成后,得到完整的`byte[]`输出到`HttpServletResponse`
> * 设置`HttpServletResponse`响应头数据,标记为文件下载
>
>
>
### 二、Vue前端调用后端接口实现下载
/**
* 导出二维码数据
*/
export const exportQrcode = async (name, params) => {
const data = await defHttp.get({ url: Api.exportQrcode, params, responseType: ‘blob’, timeout: 30000 }, { isTransformResponse: false })
if (!data) {
createMessage.warning(‘文件下载失败’)
return
}
if (!name || typeof name != ‘string’) {
name = ‘导出文件’
}
const blobOptions = { type: ‘application/octet-stream’ }
const fileSuffix = ‘.zip’
debugger
if (typeof window.navigator.msSaveBlob !== ‘undefined’) {
window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix)
} else {
const url = window.URL.createObjectURL(new Blob([data], blobOptions))
const link = document.createElement(‘a’)
link.style.display = ‘none’
link.href = url
link.setAttribute(‘download’, name + fileSuffix)
document.body.appendChild(link)
link.click()
document.body.removeChild(link) //下载完成移除元素
window.URL.revokeObjectURL(url) //释放掉blob对象
}
}
>
> * 调用后端接口,设置`responseType: 'blob'`
> * 通过`window.navigator.msSaveBlob`下载文件
>
>
>
### 最后
除了简历做到位,面试题也必不可少,整理了些题目,前面有117道汇总的面试到的题目,后面包括了HTML、CSS、JS、ES6、vue、微信小程序、项目类问题、笔试编程类题等专题。
![](https://img-blog.csdnimg.cn/img_convert/64ce8432d0f753e43f93972ad19fcd72.png)
![](https://img-blog.csdnimg.cn/img_convert/d508b1ae6a5e7d5e7d5daf668e9eccea.png)
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0