打包策略改变
This commit is contained in:
parent
34d737ad34
commit
dce2c309e5
|
|
@ -3,6 +3,7 @@ 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;
|
||||
|
|
@ -28,8 +29,10 @@ 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.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -115,7 +118,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||
|
||||
// 预生成所有二维码数据和记录,避免在写入ZIP过程中出现异常
|
||||
List<ShopTableCode> codeList = new ArrayList<>();
|
||||
List<byte[]> qrBytesList = new ArrayList<>();
|
||||
List<File> tempFiles = new ArrayList<>();
|
||||
List<String> fileNames = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
|
|
@ -128,11 +131,16 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||
|
||||
// 生成二维码图片
|
||||
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
|
||||
qrBytesList.add(baos.toByteArray());
|
||||
baos.close();
|
||||
fileNames.add(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());
|
||||
}
|
||||
|
||||
// 先保存数据库记录,确保生成ZIP前无异常
|
||||
|
|
@ -143,21 +151,30 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
|
||||
response.setCharacterEncoding(request.getCharacterEncoding());
|
||||
|
||||
// 写入ZIP文件
|
||||
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream(), Charset.forName("GBK"))) {
|
||||
// 使用 Hutool 压缩工具创建 ZIP 文件
|
||||
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream(), StandardCharsets.UTF_8)) {
|
||||
// 打开每个文件并将其添加到 ZIP 文件
|
||||
for (int i = 0; i < num; i++) {
|
||||
ZipEntry entry = new ZipEntry(fileNames.get(i));
|
||||
zipOut.putNextEntry(entry);
|
||||
File tempFile = tempFiles.get(i);
|
||||
String fileName = fileNames.get(i);
|
||||
|
||||
byte[] byteArray = qrBytesList.get(i);
|
||||
zipOut.write(byteArray, 0, byteArray.length);
|
||||
// 将文件添加到 ZIP 中
|
||||
zipOut.putNextEntry(new ZipEntry(fileName));
|
||||
byte[] fileBytes = FileUtil.readBytes(tempFile);
|
||||
zipOut.write(fileBytes);
|
||||
zipOut.flush();
|
||||
// 确保每个条目被正确关闭
|
||||
zipOut.closeEntry();
|
||||
}
|
||||
|
||||
// 完成压缩并输出
|
||||
zipOut.finish();
|
||||
zipOut.flush();
|
||||
}
|
||||
|
||||
// 删除临时文件
|
||||
for (File tempFile : tempFiles) {
|
||||
FileUtil.del(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue