|
...
|
...
|
@@ -46,6 +46,7 @@ import java.math.BigDecimal; |
|
46
|
46
|
import java.math.RoundingMode;
|
|
47
|
47
|
import java.nio.file.Files;
|
|
48
|
48
|
import java.nio.file.Path;
|
|
|
49
|
+import java.nio.file.Paths;
|
|
49
|
50
|
import java.time.format.DateTimeFormatter;
|
|
50
|
51
|
import java.util.ArrayList;
|
|
51
|
52
|
import java.util.Comparator;
|
|
...
|
...
|
@@ -201,8 +202,8 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
201
|
202
|
*/
|
|
202
|
203
|
@ApiOperation("发货单批量打印")
|
|
203
|
204
|
@GetMapping("/batchPrintShipmentsOrder")
|
|
204
|
|
- public void batchPrintShipmentsOrder(@Valid ExportShipmentsOrderVo vo, HttpServletResponse response) {
|
|
205
|
|
- List<ShipmentsOrderInfo> orderInfoList = shipmentsOrderInfoService.listByIds(vo.getIds());
|
|
|
205
|
+ public void batchPrintShipmentsOrder(@Valid @RequestBody ExportShipmentsOrderVo vo, HttpServletResponse response) {
|
|
|
206
|
+ List<ShipmentsOrderInfo> orderInfoList = shipmentsOrderInfoService.queryByIds(vo.getIds());
|
|
206
|
207
|
if (CollectionUtils.isEmpty(orderInfoList)) {
|
|
207
|
208
|
throw new DefaultClientException("发货单不存在!");
|
|
208
|
209
|
}
|
|
...
|
...
|
@@ -214,11 +215,11 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
214
|
215
|
// 创建临时目录
|
|
215
|
216
|
Path tempDir = null;
|
|
216
|
217
|
File zipFile = null;
|
|
|
218
|
+ Path baseDir = Paths.get("/tmp");
|
|
217
|
219
|
try {
|
|
218
|
220
|
// 设置响应头
|
|
219
|
|
- ResponseUtil.setZipResponseHead(response, "发货单.zip");
|
|
220
|
|
- // 创建临时目录存放Excel文件
|
|
221
|
|
- tempDir = Files.createTempDirectory("/tmp/shipments_order_export");
|
|
|
221
|
+ ResponseUtil.setZipResponseHead(response, "发货单批量打印_" + System.currentTimeMillis() + ".zip"); // 创建临时目录存放Excel文件
|
|
|
222
|
+ tempDir = Files.createTempDirectory(baseDir, "shipments_order_export");
|
|
222
|
223
|
|
|
223
|
224
|
List<String> shipmentsOrderIds = new ArrayList<>();
|
|
224
|
225
|
for (ShipmentsOrderInfo orderInfo : orderInfoList) {
|
|
...
|
...
|
@@ -247,7 +248,9 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
247
|
248
|
}
|
|
248
|
249
|
}
|
|
249
|
250
|
// 创建ZIP压缩包
|
|
250
|
|
- createZipFile(excelFiles, "发货单批量导出_" + System.currentTimeMillis() + ".zip");
|
|
|
251
|
+ zipFile = createZipFile(excelFiles, tempDir);
|
|
|
252
|
+ // 将ZIP文件写入响应流
|
|
|
253
|
+ writeZipToResponse(zipFile, response);
|
|
251
|
254
|
} catch (FileNotFoundException e) {
|
|
252
|
255
|
log.error("发货单批量打印失败: ", e);
|
|
253
|
256
|
throw new DefaultClientException("模板文件不存在: " + templatePath);
|
|
...
|
...
|
@@ -399,10 +402,12 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
399
|
402
|
|
|
400
|
403
|
|
|
401
|
404
|
/**
|
|
402
|
|
- * 创建ZIP压缩包
|
|
|
405
|
+ * 创建ZIP压缩包并返回文件对象
|
|
403
|
406
|
*/
|
|
404
|
|
- private void createZipFile(List<File> excelFiles, String zipFileName) throws IOException {
|
|
405
|
|
- File zipFile = File.createTempFile("/tmp/shipments_order_export", ".zip");
|
|
|
407
|
+ private File createZipFile(List<File> excelFiles, Path tempDir) throws IOException {
|
|
|
408
|
+ String zipFileName = "发货单批量打印_" + System.currentTimeMillis() + ".zip";
|
|
|
409
|
+ File zipFile = new File(tempDir.toFile(), zipFileName);
|
|
|
410
|
+
|
|
406
|
411
|
try (FileOutputStream fos = new FileOutputStream(zipFile);
|
|
407
|
412
|
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
408
|
413
|
byte[] buffer = new byte[1024];
|
|
...
|
...
|
@@ -418,6 +423,23 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
418
|
423
|
}
|
|
419
|
424
|
}
|
|
420
|
425
|
}
|
|
|
426
|
+ return zipFile;
|
|
|
427
|
+ }
|
|
|
428
|
+
|
|
|
429
|
+
|
|
|
430
|
+ /**
|
|
|
431
|
+ * 将ZIP文件写入HTTP响应
|
|
|
432
|
+ */
|
|
|
433
|
+ private void writeZipToResponse(File zipFile, HttpServletResponse response) throws IOException {
|
|
|
434
|
+ try (FileInputStream fis = new FileInputStream(zipFile);
|
|
|
435
|
+ OutputStream os = response.getOutputStream()) {
|
|
|
436
|
+ byte[] buffer = new byte[4096];
|
|
|
437
|
+ int bytesRead;
|
|
|
438
|
+ while ((bytesRead = fis.read(buffer)) != -1) {
|
|
|
439
|
+ os.write(buffer, 0, bytesRead);
|
|
|
440
|
+ }
|
|
|
441
|
+ os.flush();
|
|
|
442
|
+ }
|
|
421
|
443
|
}
|
|
422
|
444
|
|
|
423
|
445
|
/**
|
|
...
|
...
|
@@ -425,17 +447,17 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
425
|
447
|
*/
|
|
426
|
448
|
private void cleanupTempFiles(Path tempDir, File zipFile) {
|
|
427
|
449
|
try {
|
|
428
|
|
- // 删除临时目录
|
|
|
450
|
+ // 删除临时目录及其所有内容
|
|
429
|
451
|
if (tempDir != null && Files.exists(tempDir)) {
|
|
430
|
452
|
Files.walk(tempDir)
|
|
431
|
453
|
.sorted(Comparator.reverseOrder())
|
|
432
|
454
|
.map(Path::toFile)
|
|
433
|
455
|
.forEach(File::delete);
|
|
434
|
456
|
}
|
|
435
|
|
- // 清理
|
|
436
|
|
- if (zipFile != null && zipFile.exists()) {
|
|
437
|
|
- zipFile.delete();
|
|
438
|
|
- }
|
|
|
457
|
+ // 单独删除ZIP文件(如果存在)
|
|
|
458
|
+ if (zipFile != null && zipFile.exists()) {
|
|
|
459
|
+ zipFile.delete();
|
|
|
460
|
+ }
|
|
439
|
461
|
} catch (IOException e) {
|
|
440
|
462
|
log.warn("清理临时文件失败", e);
|
|
441
|
463
|
}
|
...
|
...
|
|