打包策略改变
This commit is contained in:
@@ -25,6 +25,7 @@ import com.mybatisflex.spring.service.impl.ServiceImpl;
|
|||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
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.awt.image.BufferedImage;
|
||||||
@@ -44,6 +45,7 @@ import java.util.zip.ZipOutputStream;
|
|||||||
* @author zs
|
* @author zs
|
||||||
* @since 2025-02-13
|
* @since 2025-02-13
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@DubboService
|
@DubboService
|
||||||
public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable> implements ShopTableService {
|
public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable> implements ShopTableService {
|
||||||
@Resource
|
@Resource
|
||||||
@@ -90,6 +92,31 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||||||
return queryChain().eq(ShopTable::getShopId, shopId).eq(ShopTable::getTableCode, tableCode).one();
|
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
|
@Override
|
||||||
public void createQrCode(Long shopId, Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
|
public void createQrCode(Long shopId, Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
|
||||||
// 查询 shop 下面有多少台桌
|
// 查询 shop 下面有多少台桌
|
||||||
@@ -154,21 +181,8 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||||||
String tempZipPath = tempDir + ".zip";
|
String tempZipPath = tempDir + ".zip";
|
||||||
ZipUtil.zip(tempDir, tempZipPath);
|
ZipUtil.zip(tempDir, tempZipPath);
|
||||||
|
|
||||||
// 读取 ZIP 文件并写入响应流
|
downloadFile(request, response, new File(tempZipPath), true);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除临时 ZIP 文件和二维码图片目录
|
|
||||||
File tempZipFile = new File(tempZipPath);
|
|
||||||
if (tempZipFile.exists()) {
|
|
||||||
tempZipFile.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dir.exists()) {
|
if (dir.exists()) {
|
||||||
for (File file : Objects.requireNonNull(dir.listFiles())) {
|
for (File file : Objects.requireNonNull(dir.listFiles())) {
|
||||||
|
|||||||
Reference in New Issue
Block a user