打包策略改变

This commit is contained in:
张松
2025-02-28 10:51:43 +08:00
parent dce2c309e5
commit 0e15ba9580

View File

@@ -3,10 +3,10 @@ package com.czg.service.account.service.impl;
import cn.dev33.satoken.context.SaHolder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.czg.account.dto.table.ShopTableAddDTO;
import com.czg.account.dto.table.ShopTableBindDTO;
@@ -28,14 +28,12 @@ import jakarta.servlet.http.HttpServletResponse;
import org.apache.dubbo.config.annotation.DubboService;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -116,9 +114,15 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
.stream()
.collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId));
// 临时目录路径
String tempDir = "/usr/local/temp/qrcode/" + shopId;
File dir = new File(tempDir);
if (!dir.exists()) {
dir.mkdirs();
}
// 预生成所有二维码数据和记录避免在写入ZIP过程中出现异常
List<ShopTableCode> codeList = new ArrayList<>();
List<File> tempFiles = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
for (int i = 0; i < num; i++) {
@@ -129,18 +133,13 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
.setShopId(shopId)
.setTableCode(tableCode));
// 生成二维码图片
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
// 确保文件夹存在
FileUtil.mkdir("temp_qr_codes");
File tempFile = FileUtil.newFile("temp_qr_codes/" + tableCode + ".png");
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
FileUtil.writeBytes(baos.toByteArray(), tempFile);
}
// 图片保存到临时目录
File qrFile = new File(dir, tableCode + ".png");
tempFiles.add(tempFile);
fileNames.add(tempFile.getName());
// 生成二维码图片
QrCodeUtil.generate(tableCode, 300, 300, qrFile);
fileNames.add(qrFile.getAbsolutePath());
}
// 先保存数据库记录确保生成ZIP前无异常
@@ -151,33 +150,38 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
response.setCharacterEncoding(request.getCharacterEncoding());
// 使用 Hutool 压缩工具创建 ZIP 文件
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream(), StandardCharsets.UTF_8)) {
// 打开每个文件并将其添加到 ZIP 文件
for (int i = 0; i < num; i++) {
File tempFile = tempFiles.get(i);
String fileName = fileNames.get(i);
// 使用 Hutool 创建临时 ZIP 文件
String tempZipPath = tempDir + ".zip";
ZipUtil.zip(tempDir, tempZipPath);
// 将文件添加到 ZIP 中
zipOut.putNextEntry(new ZipEntry(fileName));
byte[] fileBytes = FileUtil.readBytes(tempFile);
zipOut.write(fileBytes);
zipOut.flush();
zipOut.closeEntry();
// 读取 ZIP 文件并写入响应流
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(tempZipPath));
OutputStream os = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = bis.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
// 完成压缩并输出
zipOut.finish();
zipOut.flush();
}
// 删除临时文件
for (File tempFile : tempFiles) {
FileUtil.del(tempFile);
// 删除临时 ZIP 文件和二维码图片目录
File tempZipFile = new File(tempZipPath);
if (tempZipFile.exists()) {
tempZipFile.delete();
}
if (dir.exists()) {
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isFile()) {
file.delete();
}
}
dir.delete();
}
}
private String generateCode(Integer count, Long shopId, Long id, Map<String, Long> map) {
if (count > 100) {
throw new ApiNotPrintException("桌码生成失败");