mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 05:09:21 +08:00
fix(order-import): 支持从OSS读取CSV文件
问题:订单导入任务上传文件改用OSS后,后端处理时仍尝试从本地文件系统读取文件 解决: 1. 在 OssService 中新增 downloadFile 和 getFileAsStream 方法 2. 在 ImportTaskProcessorServiceImpl 中: - 优先从本地文件系统读取CSV - 本地文件不存在时,视为OSS存储路径,从OSS下载到临时文件后再处理 - 使用 BufferedInputStream 包装以支持编码检测时的 mark/reset 操作 3. detectFileEncoding 方法改为接受 InputStream 参数
This commit is contained in:
+39
-13
@@ -33,8 +33,10 @@ import top.xyquan.ecomdmbbackend.modules.ecom.orderstatus.service.OrderStatusSer
|
|||||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
|
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService;
|
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.importtask.service.SseNotificationService;
|
import top.xyquan.ecomdmbbackend.modules.ecom.importtask.service.SseNotificationService;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.sys.file.service.OssService;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
@@ -210,6 +212,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final OrderStatusService orderStatusService;
|
private final OrderStatusService orderStatusService;
|
||||||
private final SqlSessionFactory sqlSessionFactory;
|
private final SqlSessionFactory sqlSessionFactory;
|
||||||
|
private final OssService ossService;
|
||||||
|
|
||||||
public ImportTaskProcessorServiceImpl(ImportTaskService importTaskService,
|
public ImportTaskProcessorServiceImpl(ImportTaskService importTaskService,
|
||||||
OrderService orderService,
|
OrderService orderService,
|
||||||
@@ -222,7 +225,8 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
SseNotificationService sseNotificationService,
|
SseNotificationService sseNotificationService,
|
||||||
ObjectMapper objectMapper,
|
ObjectMapper objectMapper,
|
||||||
OrderStatusService orderStatusService,
|
OrderStatusService orderStatusService,
|
||||||
SqlSessionFactory sqlSessionFactory) {
|
SqlSessionFactory sqlSessionFactory,
|
||||||
|
OssService ossService) {
|
||||||
this.importTaskService = importTaskService;
|
this.importTaskService = importTaskService;
|
||||||
this.productSpecService = productSpecService;
|
this.productSpecService = productSpecService;
|
||||||
this.orderMapper = orderMapper;
|
this.orderMapper = orderMapper;
|
||||||
@@ -233,6 +237,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.orderStatusService = orderStatusService;
|
this.orderStatusService = orderStatusService;
|
||||||
this.sqlSessionFactory = sqlSessionFactory;
|
this.sqlSessionFactory = sqlSessionFactory;
|
||||||
|
this.ossService = ossService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,9 +302,10 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
Long companyId = task.getCompanyId();
|
Long companyId = task.getCompanyId();
|
||||||
|
|
||||||
// 读取CSV文件
|
// 读取CSV文件
|
||||||
// 处理文件路径:统一转换为实际文件系统路径
|
// 处理文件路径:优先从本地文件系统读取,如果不存在则尝试从OSS下载
|
||||||
String filePath = task.getFilePath();
|
String filePath = task.getFilePath();
|
||||||
File csvFile;
|
File csvFile = null;
|
||||||
|
InputStream fileInputStream = null;
|
||||||
|
|
||||||
if (filePath.contains(":")) {
|
if (filePath.contains(":")) {
|
||||||
// 包含:的路径,认为是绝对路径(Windows路径如 C:\...)
|
// 包含:的路径,认为是绝对路径(Windows路径如 C:\...)
|
||||||
@@ -313,8 +319,27 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
csvFile = new File(filePath);
|
csvFile = new File(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!csvFile.exists() || !csvFile.isFile()) {
|
// 首先尝试从本地文件系统读取
|
||||||
throw new BusinessException("CSV文件不存在: " + task.getFilePath() + " (实际查找路径: " + csvFile.getAbsolutePath() + ")");
|
if (csvFile.exists() && csvFile.isFile()) {
|
||||||
|
// 本地文件存在,直接使用
|
||||||
|
// 使用BufferedInputStream包装以支持mark/reset(用于编码检测)
|
||||||
|
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(csvFile));
|
||||||
|
} else {
|
||||||
|
// 本地文件不存在,尝试从OSS下载
|
||||||
|
// 使用storagePath作为OSS的key(去掉开头的/)
|
||||||
|
String ossKey = filePath.startsWith("/") ? filePath.substring(1) : filePath;
|
||||||
|
|
||||||
|
// 创建临时文件
|
||||||
|
File tempFile = java.io.File.createTempFile("import-csv-", ".csv");
|
||||||
|
tempFile.deleteOnExit();
|
||||||
|
|
||||||
|
if (ossService.downloadFile(ossKey, tempFile)) {
|
||||||
|
csvFile = tempFile;
|
||||||
|
// 使用BufferedInputStream包装以支持mark/reset(用于编码检测)
|
||||||
|
fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(tempFile));
|
||||||
|
} else {
|
||||||
|
throw new BusinessException("CSV文件不存在: " + task.getFilePath() + " (本地路径: " + csvFile.getAbsolutePath() + ",OSS Key: " + ossKey + ")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
|
// 获取默认订单状态ID(尝试查找"未知状态",如果不存在则使用第一个状态)
|
||||||
@@ -327,10 +352,9 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
long lastProgressNotifyAt = 0L;
|
long lastProgressNotifyAt = 0L;
|
||||||
|
|
||||||
// 尝试多种编码读取CSV文件(常见的中文编码)
|
// 尝试多种编码读取CSV文件(常见的中文编码)
|
||||||
java.nio.charset.Charset charset = detectFileEncoding(csvFile);
|
java.nio.charset.Charset charset = detectFileEncoding(fileInputStream);
|
||||||
|
|
||||||
try (java.io.InputStreamReader reader = new java.io.InputStreamReader(
|
try (java.io.InputStreamReader reader = new java.io.InputStreamReader(fileInputStream, charset);
|
||||||
new java.io.FileInputStream(csvFile), charset);
|
|
||||||
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
|
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
|
||||||
.setHeader()
|
.setHeader()
|
||||||
.setSkipHeaderRecord(true)
|
.setSkipHeaderRecord(true)
|
||||||
@@ -1589,10 +1613,10 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
* 检测文件编码
|
* 检测文件编码
|
||||||
* 尝试多种常见的中文编码
|
* 尝试多种常见的中文编码
|
||||||
*
|
*
|
||||||
* @param file 文件
|
* @param inputStream 文件输入流(需支持mark/reset,或使用BufferedInputStream包装)
|
||||||
* @return 检测到的编码
|
* @return 检测到的编码
|
||||||
*/
|
*/
|
||||||
private java.nio.charset.Charset detectFileEncoding(File file) {
|
private java.nio.charset.Charset detectFileEncoding(InputStream inputStream) {
|
||||||
// 常见的编码列表,按优先级排序
|
// 常见的编码列表,按优先级排序
|
||||||
java.nio.charset.Charset[] charsets = {
|
java.nio.charset.Charset[] charsets = {
|
||||||
java.nio.charset.Charset.forName("GBK"), // 优先尝试GBK,因为中文CSV文件常用GBK
|
java.nio.charset.Charset.forName("GBK"), // 优先尝试GBK,因为中文CSV文件常用GBK
|
||||||
@@ -1603,12 +1627,13 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
|
|
||||||
// 读取文件的前几行来检测编码
|
// 读取文件的前几行来检测编码
|
||||||
for (java.nio.charset.Charset charset : charsets) {
|
for (java.nio.charset.Charset charset : charsets) {
|
||||||
try (java.io.InputStreamReader reader = new java.io.InputStreamReader(
|
try {
|
||||||
new java.io.FileInputStream(file), charset)) {
|
inputStream.mark(2048);
|
||||||
char[] buffer = new char[2048];
|
char[] buffer = new char[2048];
|
||||||
int read = reader.read(buffer);
|
int read = new java.io.InputStreamReader(inputStream, charset).read(buffer);
|
||||||
if (read > 0) {
|
if (read > 0) {
|
||||||
String content = new String(buffer, 0, read);
|
String content = new String(buffer, 0, read);
|
||||||
|
inputStream.reset();
|
||||||
// 简单检测:如果包含常见的中文字符,可能是正确的编码
|
// 简单检测:如果包含常见的中文字符,可能是正确的编码
|
||||||
if (content.contains("商家") || content.contains("编码") ||
|
if (content.contains("商家") || content.contains("编码") ||
|
||||||
content.contains("商品") || content.contains("订单") ||
|
content.contains("商品") || content.contains("订单") ||
|
||||||
@@ -1616,6 +1641,7 @@ public class ImportTaskProcessorServiceImpl implements ImportTaskProcessorServic
|
|||||||
return charset;
|
return charset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
inputStream.reset();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 忽略编码检测错误
|
// 忽略编码检测错误
|
||||||
}
|
}
|
||||||
|
|||||||
+17
@@ -51,5 +51,22 @@ public interface OssService {
|
|||||||
* @return 是否存在
|
* @return 是否存在
|
||||||
*/
|
*/
|
||||||
boolean fileExists(String storagePath);
|
boolean fileExists(String storagePath);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从OSS下载文件到本地临时文件
|
||||||
|
*
|
||||||
|
* @param storagePath 存储路径/Key
|
||||||
|
* @param localFile 本地目标文件
|
||||||
|
* @return 是否下载成功
|
||||||
|
*/
|
||||||
|
boolean downloadFile(String storagePath, java.io.File localFile);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取OSS文件的输入流
|
||||||
|
*
|
||||||
|
* @param storagePath 存储路径/Key
|
||||||
|
* @return 文件输入流(使用完后需自行关闭),如果失败返回null
|
||||||
|
*/
|
||||||
|
java.io.InputStream getFileAsStream(String storagePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
@@ -299,5 +299,30 @@ public class OssServiceImpl implements OssService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean downloadFile(String storagePath, java.io.File localFile) {
|
||||||
|
if (ossClient == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ossClient.getObject(ossConfig.getBucketName(), storagePath, localFile);
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public java.io.InputStream getFileAsStream(String storagePath) {
|
||||||
|
if (ossClient == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return ossClient.getObject(ossConfig.getBucketName(), storagePath).getObjectContent();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user