diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index a546096ab6..96725d6dc1 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -72,6 +72,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; +import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; @@ -2511,17 +2512,24 @@ public class TestCaseService { AttachmentModuleRelationExample relationExample = new AttachmentModuleRelationExample(); relationExample.createCriteria().andRelationTypeEqualTo(AttachmentType.TEST_CASE.type()); List relations = attachmentModuleRelationMapper.selectByExample(relationExample); - List allFileMeta = fileService.getAllFileMeta(); - List allFileContent = fileService.getAllFileContent(); Map> relationGroup = relations.stream().collect(Collectors.groupingBy(AttachmentModuleRelation::getRelationId)); for(Map.Entry> entry : relationGroup.entrySet()) { final String caseId = entry.getKey(); - final String uploadPath = FileUtils.ATTACHMENT_DIR + "/" + AttachmentType.TEST_CASE.type() + "/" + caseId; + final String uploadPath = FileUtils.ATTACHMENT_DIR + File.separator + AttachmentType.TEST_CASE.type() + File.separator + caseId; + // 获取同一用例关联的文件ID + List fileIds = entry.getValue().stream().map(AttachmentModuleRelation::getAttachmentId).collect(Collectors.toList()); + // 只在每次循环时查询目标用例下附件数据, 防止附件数据过大OOM + FileMetadataExample fileMetadataExample = new FileMetadataExample(); + fileMetadataExample.createCriteria().andIdIn(fileIds); + List allCaseFileMetadatas = fileMetadataMapper.selectByExample(fileMetadataExample); + FileContentExample fileContentExample = new FileContentExample(); + fileContentExample.createCriteria().andFileIdIn(fileIds); + List allCaseFileContents = fileContentMapper.selectByExample(fileContentExample); entry.getValue().stream().forEach(relation -> { String filename = ""; - List fileMetadatas = allFileMeta.stream().filter(fileMetadata -> fileMetadata.getId().equals(relation.getAttachmentId())) + List fileMetadatas = allCaseFileMetadatas.stream().filter(fileMetadata -> fileMetadata.getId().equals(relation.getAttachmentId())) .collect(Collectors.toList()); - List fileContents = allFileContent.stream().filter(fileContent -> fileContent.getFileId().equals(relation.getAttachmentId())) + List fileContents = allCaseFileContents.stream().filter(fileContent -> fileContent.getFileId().equals(relation.getAttachmentId())) .collect(Collectors.toList()); if (fileMetadatas.size() == 1) { FileMetadata fileMetadata = fileMetadatas.get(0);