luoweijian 1 month ago
parent
commit
5005ca261e
  1. 3
      src/main/java/com/project/information/application/KnowledgePointApplicationService.java
  2. 64
      src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java
  3. 6
      src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java
  4. 19
      src/main/java/com/project/information/utils/MinIoUtils.java

3
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.base.domain.result.Result;
import com.project.information.domain.dto.KnowledgePointStatisticsDTO; import com.project.information.domain.dto.KnowledgePointStatisticsDTO;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map; import java.util.Map;
public interface KnowledgePointApplicationService { public interface KnowledgePointApplicationService {
Result<KnowledgePointStatisticsDTO> getStatistics(Long subLineId) throws Exception; Result<KnowledgePointStatisticsDTO> getStatistics(Long subLineId) throws Exception;
void parse(Map<Long, MultipartFile> fileMap); void parse(Map<Long, String> fileMap);
} }

64
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.GetStatisticsKnowledgePointDomainService;
import com.project.information.domain.service.InformationBaseService; import com.project.information.domain.service.InformationBaseService;
import com.project.information.domain.service.KnowledgePointBaseService; import com.project.information.domain.service.KnowledgePointBaseService;
import com.project.information.utils.MinIoUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -45,6 +48,9 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli
@Autowired @Autowired
private GetStatisticsKnowledgePointDomainService getStatisticsKnowledgePointDomainService; private GetStatisticsKnowledgePointDomainService getStatisticsKnowledgePointDomainService;
@Autowired
private MinIoUtils minIoUtils;
private final RestTemplate restTemplate = new RestTemplate(); private final RestTemplate restTemplate = new RestTemplate();
@Value("${analysis.host:http://172.16.204.50}") @Value("${analysis.host:http://172.16.204.50}")
@ -61,32 +67,45 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli
@Override @Override
@Async("asycExecutor") @Async("asycExecutor")
public void parse(Map<Long, MultipartFile> fileMap) { public void parse(Map<Long, String> fileIdPathMap) {
for (Long id : fileMap.keySet()) {
for (Map.Entry<Long, String> entry : fileIdPathMap.entrySet()) {
Long id = entry.getKey();
String path = entry.getValue();
try { 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) { } catch (Exception e) {
log.error("文件解析失败:{}", fileMap.get(id).getOriginalFilename(), e); log.error("解析异常, ID:{}", id, e);
//更新资料状态 // 失败更新状态
updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); 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(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(MediaType.parseMediaTypes("*/*")); headers.setAccept(MediaType.parseMediaTypes("*/*"));
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
ByteArrayResource fileResource = new ByteArrayResource(file.getBytes()) { // 1. 将 InputStream 转换为字节数组 (注意:此处会消耗内存,大小由文件决定)
byte[] bytes = IOUtils.toByteArray(inputStream);
// 2. 封装为 Resource
ByteArrayResource fileResource = new ByteArrayResource(bytes) {
@Override @Override
public String getFilename() { public String getFilename() {
return file.getOriginalFilename(); return fileName; // 使用传入的文件名
} }
}; };
@ -95,23 +114,26 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli
HttpEntity<MultiValueMap<String, Object>> requestEntity = HttpEntity<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<>(body, headers); new HttpEntity<>(body, headers);
// 3. 发送请求
log.info("正在发起文件 [{}] 的解析请求,ID: {}", fileName, id);
ResponseEntity<String> response = ResponseEntity<String> 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<KnowledgePointEntity> knowledgePointEntities = parseAnalysisResponse(response, id);
List<KnowledgePointEntity> knowledgePointEntities = parseAnalysisResponse(response,id); if (!CollectionUtils.isEmpty(knowledgePointEntities)) {
if(!CollectionUtils.isEmpty(knowledgePointEntities)){
knowledgePointBaseService.saveBatch(knowledgePointEntities); knowledgePointBaseService.saveBatch(knowledgePointEntities);
//更新状态 // 更新资料状态为:成功
updateStatuInformation(knowledgePointEntities.get(0).getParseName(),InformationParseStatusEnum.Success.getValue(),id); updateStatusInformation(knowledgePointEntities.get(0).getParseName(), InformationParseStatusEnum.Success.getValue(), id);
}else{ } else {
log.error("获取知识点失败:{}", "不存在知识点"); log.warn("文件 [{}] 解析成功但未提取到知识点", fileName);
//更新状态 // 更新资料状态为:成功 (或根据业务定义为无知识点状态)
updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); 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 informationEntity = new InformationEntity();
informationEntity.setId(id); informationEntity.setId(id);
if (StringUtils.isNotBlank(pareName)){ if (StringUtils.isNotBlank(pareName)){

6
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()) { if (Objects.isNull(productLine) || !productLine.getLeaf()) {
throw new BusinessErrorException("不存在的子产品线"); throw new BusinessErrorException("不存在的子产品线");
} }
List<String> successFiles = new ArrayList<>(); List<String> successFiles = new ArrayList<>();
Map<Long, MultipartFile> fileMap = new HashMap<>(); Map<Long, String> fileMap = new HashMap<>();
for (MultipartFile file : files) { for (MultipartFile file : files) {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
@ -128,7 +130,7 @@ public class UploadInformationDomainServiceImpl implements UploadInformationDoma
informationBaseService.save(entity); informationBaseService.save(entity);
//收集file //收集file
fileMap.put(entity.getId(), file); fileMap.put(entity.getId(), filePath);
} }
//发起解析文档知识点 //发起解析文档知识点

19
src/main/java/com/project/information/utils/MinIoUtils.java

@ -146,6 +146,25 @@ public class MinIoUtils {
return null; 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 * 简化的InputStream上传方法自动设置ContentType
* *

Loading…
Cancel
Save