From 5005ca261ec7ef03bf21ad9f316ba21906908273 Mon Sep 17 00:00:00 2001 From: luoweijian <1329394916@qq.com> Date: Thu, 19 Mar 2026 09:47:00 +0800 Subject: [PATCH] bug --- .../KnowledgePointApplicationService.java | 3 +- .../KnowledgePointApplicationServiceImpl.java | 64 +++++++++++++------ .../UploadInformationDomainServiceImpl.java | 6 +- .../project/information/utils/MinIoUtils.java | 19 ++++++ 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/project/information/application/KnowledgePointApplicationService.java b/src/main/java/com/project/information/application/KnowledgePointApplicationService.java index fa239d6..e01244d 100644 --- a/src/main/java/com/project/information/application/KnowledgePointApplicationService.java +++ b/src/main/java/com/project/information/application/KnowledgePointApplicationService.java @@ -2,12 +2,11 @@ package com.project.information.application; import com.project.base.domain.result.Result; import com.project.information.domain.dto.KnowledgePointStatisticsDTO; -import org.springframework.web.multipart.MultipartFile; import java.util.Map; public interface KnowledgePointApplicationService { Result getStatistics(Long subLineId) throws Exception; - void parse(Map fileMap); + void parse(Map fileMap); } diff --git a/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java b/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java index a4f90f2..30aa484 100644 --- a/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java +++ b/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java @@ -12,7 +12,9 @@ import com.project.information.domain.enums.InformationParseStatusEnum; import com.project.information.domain.service.GetStatisticsKnowledgePointDomainService; import com.project.information.domain.service.InformationBaseService; import com.project.information.domain.service.KnowledgePointBaseService; +import com.project.information.utils.MinIoUtils; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +32,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; @@ -45,6 +48,9 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli @Autowired private GetStatisticsKnowledgePointDomainService getStatisticsKnowledgePointDomainService; + @Autowired + private MinIoUtils minIoUtils; + private final RestTemplate restTemplate = new RestTemplate(); @Value("${analysis.host:http://172.16.204.50}") @@ -61,32 +67,45 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli @Override @Async("asycExecutor") - public void parse(Map fileMap) { - for (Long id : fileMap.keySet()) { + public void parse(Map fileIdPathMap) { + + for (Map.Entry entry : fileIdPathMap.entrySet()) { + Long id = entry.getKey(); + String path = entry.getValue(); try { - uploadToPython(fileMap.get(id),id); + // 从 MinIO 获取流再发给 Python + try (InputStream inputStream = minIoUtils.getObject(path)) { + uploadToPython(inputStream, path , id); + } + // 成功后更新状态 + updateStatusInformation(null, InformationParseStatusEnum.Success.getValue(), id); } catch (Exception e) { - log.error("文件解析失败:{}", fileMap.get(id).getOriginalFilename(), e); - //更新资料状态 - updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); + log.error("解析异常, ID:{}", id, e); + // 失败更新状态 + updateStatusInformation(null, InformationParseStatusEnum.Fail.getValue(), id); } } + } /** * 发送请求,解析文档知识点 */ - private void uploadToPython(MultipartFile file,Long id) throws Exception { + private void uploadToPython(InputStream inputStream, String fileName, Long id) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setAccept(MediaType.parseMediaTypes("*/*")); MultiValueMap body = new LinkedMultiValueMap<>(); - ByteArrayResource fileResource = new ByteArrayResource(file.getBytes()) { + // 1. 将 InputStream 转换为字节数组 (注意:此处会消耗内存,大小由文件决定) + byte[] bytes = IOUtils.toByteArray(inputStream); + + // 2. 封装为 Resource + ByteArrayResource fileResource = new ByteArrayResource(bytes) { @Override public String getFilename() { - return file.getOriginalFilename(); + return fileName; // 使用传入的文件名 } }; @@ -95,23 +114,26 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli HttpEntity> requestEntity = new HttpEntity<>(body, headers); + // 3. 发送请求 + log.info("正在发起文件 [{}] 的解析请求,ID: {}", fileName, id); ResponseEntity response = - restTemplate.postForEntity(analysisHost+":"+analysisPort+analysisUrl, requestEntity, String.class); + restTemplate.postForEntity(analysisHost + ":" + analysisPort + analysisUrl, requestEntity, String.class); + + log.info("文件 [{}] 解析请求成功,返回结果:{}", fileName, response.getBody()); - log.info("文件 [{}] 解析请求成功,返回结果:{}", file.getOriginalFilename(), response.getBody()); + // 4. 解析 Python 返回的结果并入库 + List knowledgePointEntities = parseAnalysisResponse(response, id); - List knowledgePointEntities = parseAnalysisResponse(response,id); - if(!CollectionUtils.isEmpty(knowledgePointEntities)){ + if (!CollectionUtils.isEmpty(knowledgePointEntities)) { knowledgePointBaseService.saveBatch(knowledgePointEntities); - //更新状态 - updateStatuInformation(knowledgePointEntities.get(0).getParseName(),InformationParseStatusEnum.Success.getValue(),id); - }else{ - log.error("获取知识点失败:{}", "不存在知识点"); - //更新状态 - updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); + // 更新资料状态为:成功 + updateStatusInformation(knowledgePointEntities.get(0).getParseName(), InformationParseStatusEnum.Success.getValue(), id); + } else { + log.warn("文件 [{}] 解析成功但未提取到知识点", fileName); + // 更新资料状态为:成功 (或根据业务定义为无知识点状态) + updateStatusInformation(null, InformationParseStatusEnum.Success.getValue(), id); } } - /** * 解析返回数据 */ @@ -178,7 +200,7 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli /** * 更新文件解析状态 */ - private void updateStatuInformation(String pareName,Integer status,Long id){ + private void updateStatusInformation(String pareName,Integer status,Long id){ InformationEntity informationEntity = new InformationEntity(); informationEntity.setId(id); if (StringUtils.isNotBlank(pareName)){ diff --git a/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java b/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java index 95c19a2..b5c39e7 100644 --- a/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java +++ b/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java @@ -94,8 +94,10 @@ public class UploadInformationDomainServiceImpl implements UploadInformationDoma if (Objects.isNull(productLine) || !productLine.getLeaf()) { throw new BusinessErrorException("不存在的子产品线"); } + + List successFiles = new ArrayList<>(); - Map fileMap = new HashMap<>(); + Map fileMap = new HashMap<>(); for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); @@ -128,7 +130,7 @@ public class UploadInformationDomainServiceImpl implements UploadInformationDoma informationBaseService.save(entity); //收集file - fileMap.put(entity.getId(), file); + fileMap.put(entity.getId(), filePath); } //发起解析文档知识点 diff --git a/src/main/java/com/project/information/utils/MinIoUtils.java b/src/main/java/com/project/information/utils/MinIoUtils.java index 2f008af..a008fc6 100644 --- a/src/main/java/com/project/information/utils/MinIoUtils.java +++ b/src/main/java/com/project/information/utils/MinIoUtils.java @@ -146,6 +146,25 @@ public class MinIoUtils { return null; } } + + /** + * 获取文件输入流 + * @param objectName 文件在 MinIO 中的路径/名称 + * @return InputStream + */ + public InputStream getObject(String objectName) { + try { + return customMinioClient.getObject( + GetObjectArgs.builder() + .bucket(minioProperties.getBucket()) // 确保你能获取到 bucket 配置 + .object(objectName) + .build() + ); + } catch (Exception e) { + log.error("从 MinIO 获取文件流失败, objectName: {}", objectName, e); + throw new RuntimeException("文件获取失败", e); + } + } /** * 简化的InputStream上传方法(自动设置ContentType) *