桌码打包下载失败修复

This commit is contained in:
张松 2025-02-27 18:57:21 +08:00
parent 67c17e6f92
commit 9f035cd1fa
1 changed files with 9 additions and 8 deletions

View File

@ -105,7 +105,8 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip"); response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
ArrayList<ShopTableCode> codeList = new ArrayList<>(); ArrayList<ShopTableCode> codeList = new ArrayList<>();
// 使用 ZipOutputStream 将二维码写入 zip
// 使用 ZipOutputStream 直接将二维码写入 ZIP
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) { try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
String tableCode = generateCode(1, shopId, ++maxId, codeMap); String tableCode = generateCode(1, shopId, ++maxId, codeMap);
@ -113,25 +114,25 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
codeList.add(new ShopTableCode().setShopId(shopId).setTableCode(tableCode)); codeList.add(new ShopTableCode().setShopId(shopId).setTableCode(tableCode));
// 生成二维码并写入 ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 生成二维码 // 生成二维码
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300); BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
// 转换为字节数组 // 转换为字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream(); ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, byteArrayOutputStream);
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, out); byte[] qrBytes = byteArrayOutputStream.toByteArray();
out.flush(); // 确保所有数据都已写入
byte[] qrBytes = out.toByteArray();
// 添加到 ZIP // 创建一个新的 ZipEntry
zipOut.putNextEntry(new ZipEntry(tableCode + ".png")); zipOut.putNextEntry(new ZipEntry(tableCode + ".png"));
zipOut.write(qrBytes); zipOut.write(qrBytes);
zipOut.closeEntry(); // 确保当前文件条目被关闭 zipOut.closeEntry(); // 确保当前文件条目被关闭
} }
// 刷新并完成文件输出 // 刷新并完成文件输出
zipOut.finish(); zipOut.finish();
} }
// 保存二维码信息
shopTableCodeService.saveBatch(codeList); shopTableCodeService.saveBatch(codeList);
} }