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,60 +309,62 @@ 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()); // 获取文件信息
if (fileInfo == null) { SysFileVO fileInfo = sysFileService.getFileById(task.getFileId());
throw new BusinessException("文件不存在,fileId: " + task.getFileId()); if (fileInfo == null) {
} throw new BusinessException("文件不存在,fileId: " + task.getFileId());
}
String storagePath = fileInfo.getStoragePath();
String storageType = fileInfo.getStorageType(); String storagePath = fileInfo.getStoragePath();
String storageType = fileInfo.getStorageType();
if ("local".equals(storageType)) { InputStream fileInputStream;
// 本地存储:使用 storagePath 作为本地文件路径
File localFile = new File(storagePath); if ("local".equals(storageType)) {
if (localFile.exists() && localFile.isFile()) { // 本地存储:使用 storagePath 作为本地文件路径
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(localFile)); File localFile = new File(storagePath);
csvFile = localFile; if (localFile.exists() && localFile.isFile()) {
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(localFile));
csvFile = localFile;
} else {
throw new BusinessException("本地文件不存在: " + storagePath);
}
} else { } 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 下载到临时文件 // 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
File tempFile = java.io.File.createTempFile("import-csv-", ".csv"); 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)) { try (java.io.InputStreamReader reader = new java.io.InputStreamReader(fileInputStream, charset);
throw new BusinessException("从对象存储下载文件失败,storagePath: " + storagePath); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
} .setHeader()
.setSkipHeaderRecord(true)
csvFile = tempFile; .setIgnoreHeaderCase(true)
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempFile)); .setTrim(true) // 自动去除表头和值的前后空格
} .build())) {
// 获取默认订单状态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())) {
List<CSVRecord> records = csvParser.getRecords(); List<CSVRecord> records = csvParser.getRecords();
totalCount = records.size(); totalCount = records.size();
// 更新总行数 // 更新总行数
@@ -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