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