fix(order-import): 完善OSS临时文件清理和流关闭

- 通过 sysFileService.getFileById 获取文件信息
- 根据 storageType 判断存储类型,本地直读,OSS下载到临时文件
- 在 finally 块中清理从OSS下载的临时文件
- 注意:fileInputStream 由 try-with-resources 中的 reader 关闭,
  实际关闭顺序为 reader → csvParser → fileInputStream
This commit is contained in:
小蟑螂
2026-03-29 14:18:00 +08:00
parent bee92ce7ce
commit 05d13c3f35
2 changed files with 56 additions and 48 deletions
@@ -309,8 +309,9 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
// 读取CSV文件 // 读取CSV文件
// 通过 fileId 从统一文件系统获取文件信息,根据存储类型读取文件 // 通过 fileId 从统一文件系统获取文件信息,根据存储类型读取文件
File csvFile = null; File csvFile = null;
InputStream fileInputStream = null; File tempOssFile = null; // 仅当从OSS下载时使用,用于后续清理
try {
// 获取文件信息 // 获取文件信息
SysFileVO fileInfo = sysFileService.getFileById(task.getFileId()); SysFileVO fileInfo = sysFileService.getFileById(task.getFileId());
if (fileInfo == null) { if (fileInfo == null) {
@@ -319,6 +320,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
String storagePath = fileInfo.getStoragePath(); String storagePath = fileInfo.getStoragePath();
String storageType = fileInfo.getStorageType(); String storageType = fileInfo.getStorageType();
InputStream fileInputStream;
if ("local".equals(storageType)) { if ("local".equals(storageType)) {
// 本地存储:使用 storagePath 作为本地文件路径 // 本地存储:使用 storagePath 作为本地文件路径
@@ -331,14 +333,14 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
} }
} else { } else {
// 对象存储(oss/s3/qiniu/cos等):从 OSS 下载到临时文件 // 对象存储(oss/s3/qiniu/cos等):从 OSS 下载到临时文件
File tempFile = java.io.File.createTempFile("import-csv-", ".csv"); tempOssFile = java.io.File.createTempFile("import-csv-", ".csv");
if (!ossService.downloadFile(storagePath, tempFile)) { if (!ossService.downloadFile(storagePath, tempOssFile)) {
throw new BusinessException("从对象存储下载文件失败,storagePath: " + storagePath); throw new BusinessException("从对象存储下载文件失败,storagePath: " + storagePath);
} }
csvFile = tempFile; csvFile = tempOssFile;
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempFile)); fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempOssFile));
} }
// 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态) // 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
@@ -415,6 +417,12 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
successCount += flushResult[0]; successCount += flushResult[0];
failCount += flushResult[1]; failCount += flushResult[1];
} }
} finally {
// 清理:从OSS下载的临时文件需要手动删除
if (tempOssFile != null && tempOssFile.exists()) {
tempOssFile.delete();
}
}
// 更新最终状态 // 更新最终状态
String finalStatus = failCount == 0 ? "success" : (successCount > 0 ? "success" : "failed"); String finalStatus = failCount == 0 ? "success" : (successCount > 0 ? "success" : "failed");
Binary file not shown.

After

Width:  |  Height:  |  Size: 791 KiB