打包策略改变

This commit is contained in:
张松 2025-02-28 10:59:24 +08:00
parent 7a477b3baf
commit e0d5db8d13
1 changed files with 28 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import java.awt.image.BufferedImage;
@ -44,6 +45,7 @@ import java.util.zip.ZipOutputStream;
* @author zs
* @since 2025-02-13
*/
@Slf4j
@DubboService
public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable> implements ShopTableService {
@Resource
@ -90,6 +92,31 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
return queryChain().eq(ShopTable::getShopId, shopId).eq(ShopTable::getTableCode, tableCode).one();
}
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file, boolean deleteOnExit) {
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
IoUtil.copy(fis, response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (fis != null) {
try {
fis.close();
if (deleteOnExit) {
file.deleteOnExit();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
@Override
public void createQrCode(Long shopId, Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
// 查询 shop 下面有多少台桌
@ -154,21 +181,8 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
String tempZipPath = tempDir + ".zip";
ZipUtil.zip(tempDir, tempZipPath);
// 读取 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);
}
}
downloadFile(request, response, new File(tempZipPath), true);
// 删除临时 ZIP 文件和二维码图片目录
File tempZipFile = new File(tempZipPath);
if (tempZipFile.exists()) {
tempZipFile.delete();
}
if (dir.exists()) {
for (File file : Objects.requireNonNull(dir.listFiles())) {