mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
fix(order-import): 完善OSS临时文件清理和流关闭
- 通过 sysFileService.getFileById 获取文件信息 - 根据 storageType 判断存储类型,本地直读,OSS下载到临时文件 - 在 finally 块中清理从OSS下载的临时文件 - 注意:fileInputStream 由 try-with-resources 中的 reader 关闭, 实际关闭顺序为 reader → csvParser → fileInputStream
This commit is contained in:
+56
-48
@@ -309,60 +309,62 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
// 读取CSV文件
|
||||
// 通过 fileId 从统一文件系统获取文件信息,根据存储类型读取文件
|
||||
File csvFile = null;
|
||||
InputStream fileInputStream = null;
|
||||
File tempOssFile = null; // 仅当从OSS下载时使用,用于后续清理
|
||||
|
||||
// 获取文件信息
|
||||
SysFileVO fileInfo = sysFileService.getFileById(task.getFileId());
|
||||
if (fileInfo == null) {
|
||||
throw new BusinessException("文件不存在,fileId: " + task.getFileId());
|
||||
}
|
||||
|
||||
String storagePath = fileInfo.getStoragePath();
|
||||
String storageType = fileInfo.getStorageType();
|
||||
|
||||
if ("local".equals(storageType)) {
|
||||
// 本地存储:使用 storagePath 作为本地文件路径
|
||||
File localFile = new File(storagePath);
|
||||
if (localFile.exists() && localFile.isFile()) {
|
||||
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(localFile));
|
||||
csvFile = localFile;
|
||||
try {
|
||||
// 获取文件信息
|
||||
SysFileVO fileInfo = sysFileService.getFileById(task.getFileId());
|
||||
if (fileInfo == null) {
|
||||
throw new BusinessException("文件不存在,fileId: " + task.getFileId());
|
||||
}
|
||||
|
||||
String storagePath = fileInfo.getStoragePath();
|
||||
String storageType = fileInfo.getStorageType();
|
||||
InputStream fileInputStream;
|
||||
|
||||
if ("local".equals(storageType)) {
|
||||
// 本地存储:使用 storagePath 作为本地文件路径
|
||||
File localFile = new File(storagePath);
|
||||
if (localFile.exists() && localFile.isFile()) {
|
||||
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(localFile));
|
||||
csvFile = localFile;
|
||||
} else {
|
||||
throw new BusinessException("本地文件不存在: " + storagePath);
|
||||
}
|
||||
} else {
|
||||
throw new BusinessException("本地文件不存在: " + storagePath);
|
||||
// 对象存储(oss/s3/qiniu/cos等):从 OSS 下载到临时文件
|
||||
tempOssFile = java.io.File.createTempFile("import-csv-", ".csv");
|
||||
|
||||
if (!ossService.downloadFile(storagePath, tempOssFile)) {
|
||||
throw new BusinessException("从对象存储下载文件失败,storagePath: " + storagePath);
|
||||
}
|
||||
|
||||
csvFile = tempOssFile;
|
||||
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempOssFile));
|
||||
}
|
||||
} else {
|
||||
// 对象存储(oss/s3/qiniu/cos等):从 OSS 下载到临时文件
|
||||
File tempFile = java.io.File.createTempFile("import-csv-", ".csv");
|
||||
|
||||
// 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
|
||||
Long defaultStatusId = getDefaultOrderStatusId(companyId);
|
||||
|
||||
int totalCount = 0;
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
StringBuilder errorMessages = new StringBuilder();
|
||||
long lastProgressNotifyAt = 0L;
|
||||
|
||||
// 尝试多种编码读取CSV文件(常见的中文编码)
|
||||
java.nio.charset.Charset charset = detectFileEncoding(fileInputStream);
|
||||
|
||||
if (!ossService.downloadFile(storagePath, tempFile)) {
|
||||
throw new BusinessException("从对象存储下载文件失败,storagePath: " + storagePath);
|
||||
}
|
||||
|
||||
csvFile = tempFile;
|
||||
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempFile));
|
||||
}
|
||||
|
||||
// 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
|
||||
Long defaultStatusId = getDefaultOrderStatusId(companyId);
|
||||
|
||||
int totalCount = 0;
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
StringBuilder errorMessages = new StringBuilder();
|
||||
long lastProgressNotifyAt = 0L;
|
||||
|
||||
// 尝试多种编码读取CSV文件(常见的中文编码)
|
||||
java.nio.charset.Charset charset = detectFileEncoding(fileInputStream);
|
||||
|
||||
try (java.io.InputStreamReader reader = new java.io.InputStreamReader(fileInputStream, charset);
|
||||
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
|
||||
.setHeader()
|
||||
.setSkipHeaderRecord(true)
|
||||
.setIgnoreHeaderCase(true)
|
||||
.setTrim(true) // 自动去除表头和值的前后空格
|
||||
.build())) {
|
||||
try (java.io.InputStreamReader reader = new java.io.InputStreamReader(fileInputStream, charset);
|
||||
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
|
||||
.setHeader()
|
||||
.setSkipHeaderRecord(true)
|
||||
.setIgnoreHeaderCase(true)
|
||||
.setTrim(true) // 自动去除表头和值的前后空格
|
||||
.build())) {
|
||||
|
||||
|
||||
List<CSVRecord> records = csvParser.getRecords();
|
||||
List<CSVRecord> records = csvParser.getRecords();
|
||||
totalCount = records.size();
|
||||
|
||||
// 更新总行数
|
||||
@@ -415,6 +417,12 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
||||
successCount += flushResult[0];
|
||||
failCount += flushResult[1];
|
||||
}
|
||||
} finally {
|
||||
// 清理:从OSS下载的临时文件需要手动删除
|
||||
if (tempOssFile != null && tempOssFile.exists()) {
|
||||
tempOssFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
// 更新最终状态
|
||||
String finalStatus = failCount == 0 ? "success" : (successCount > 0 ? "success" : "failed");
|
||||
|
||||
Reference in New Issue
Block a user