打包策略改变

This commit is contained in:
张松
2025-02-28 11:27:57 +08:00
parent 8e400786f1
commit 4b8e28876a

View File

@@ -28,16 +28,12 @@ import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.config.annotation.DubboService;
import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* 台桌配置 服务层实现。 * 台桌配置 服务层实现。
@@ -108,7 +104,12 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
try { try {
fis.close(); fis.close();
if (deleteOnExit) { if (deleteOnExit) {
file.deleteOnExit(); if (file.exists()) {
boolean delete = file.delete();
if (delete) {
log.info("删除临时qrcode文件成功");
}
}
} }
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@@ -142,15 +143,17 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
.collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId)); .collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId));
// 临时目录路径 // 临时目录路径
String tempDir = "/usr/local/temp/qrcode/" + shopId; String tempDir = "/usr/local/temp/qrcode/%d".formatted(shopId);
File dir = new File(tempDir); File dir = new File(tempDir);
if (!dir.exists()) { if (!dir.exists()) {
dir.mkdirs(); if (!dir.mkdirs()) {
log.error("创建临时目录失败");
throw new ApiNotPrintException("生成失败,请稍后再试");
}
} }
// 预生成所有二维码数据和记录避免在写入ZIP过程中出现异常 // 预生成所有二维码数据和记录避免在写入ZIP过程中出现异常
List<ShopTableCode> codeList = new ArrayList<>(); List<ShopTableCode> codeList = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
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);
@@ -165,8 +168,6 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
// 生成二维码图片 // 生成二维码图片
QrCodeUtil.generate(tableCode, 300, 300, qrFile); QrCodeUtil.generate(tableCode, 300, 300, qrFile);
fileNames.add(qrFile.getAbsolutePath());
} }
// 先保存数据库记录确保生成ZIP前无异常 // 先保存数据库记录确保生成ZIP前无异常
@@ -178,7 +179,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
response.setCharacterEncoding(request.getCharacterEncoding()); response.setCharacterEncoding(request.getCharacterEncoding());
// 使用 Hutool 创建临时 ZIP 文件 // 使用 Hutool 创建临时 ZIP 文件
String tempZipPath = tempDir + ".zip"; String tempZipPath = "%s.zip".formatted(tempDir);
ZipUtil.zip(tempDir, tempZipPath); ZipUtil.zip(tempDir, tempZipPath);
downloadFile(request, response, new File(tempZipPath), true); downloadFile(request, response, new File(tempZipPath), true);
@@ -188,13 +189,13 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
if (dir.exists()) { if (dir.exists()) {
for (File file : Objects.requireNonNull(dir.listFiles())) { for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isFile()) { if (file.isFile()) {
file.delete(); if (!file.delete()) {
log.info("删除临时qrcode文件失败");
}
} }
} }
dir.delete();
} }
} }
private String generateCode(Integer count, Long shopId, Long id, Map<String, Long> map) { private String generateCode(Integer count, Long shopId, Long id, Map<String, Long> map) {