1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
@Service("ofdPathUploadStrategy") @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class OdfPathUploadStrategyImpl extends AbstractPathUploadStrategyImpl{
private static Logger LOG = LoggerFactory.getLogger(OdfPathUploadStrategyImpl.class); private final AppraisalFileRepository appraisalFileRepository;
private final AppraisalFileService appraisalFileService;
private final IOssEndPoint ossEndPoint;
@Override void uploadByPath(PathUpLoadMessage message, String strategy) throws IOException {
final File file = new File(message.getFile()); final String folderName = file.getName(); final Long taskId = Convert.toLong(message.getTaskId()); final String tenantId = message.getTenantId(); final Boolean overWrite = Convert.toBool(message.getOverWrite()); LOG.info("当前进行服务器路径上传OFD的文件夹是{}", folderName); final List<File> pdfFiles = getAllPDFFiles(file, List.of("ofd")); long timeStart = DateUtil.current(); long countStart = DateUtil.current(); LOG.info("开始解析文件夹{},包含{}个OFD文件", folderName, pdfFiles.size());
for (int fileIndex = 0; fileIndex < pdfFiles.size(); fileIndex++) { File ofdFile = pdfFiles.get(fileIndex); String fileName = ofdFile.getName(); List<AppraisalFile> existingFiles = appraisalFileRepository.findByTaskIdAndFileName(taskId, fileName); if (!existingFiles.isEmpty()) { if (overWrite) { List<Long> existingIds = existingFiles.stream().map(AppraisalFile::getId).collect(Collectors.toList()); appraisalFileService.deleteExistingFiles(existingIds); } else { LOG.info("已存在的文件将被跳过: {}", fileName); continue; } } Attach ofdAttach = null; try { try (FileInputStream fileInputStream1 = new FileInputStream(ofdFile)) { ofdAttach = ossEndPoint.putAttachWithTenant( new InMemoryMultipartFile(fileName, fileInputStream1), tenantId ).getData(); }
try (FileInputStream fileInputStream2 = new FileInputStream(ofdFile)) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ConvertHelper.toPdf(fileInputStream2, stream); Attach pdfAttach = ossEndPoint.putAttach(new InMemoryMultipartFile(IdUtil.randomUUID() + ".pdf", stream.toByteArray()) ).getData();
AppraisalFile appraisalFile = appraisalFileService.createAppraisalFile(taskId, fileName, pdfAttach); appraisalFile.setOfdAttach(ofdAttach.getId()); appraisalFile.setFileType(InferFileType.OFD); appraisalFileRepository.save(appraisalFile);
LOG.info("上传OFD成功所在文件夹名称{} - 当前进度: {}/{},文件名: {},共已耗时: {}s",file.getName(), fileIndex+1, pdfFiles.size(), fileName, (DateUtil.current() - timeStart) / 1000.0); } } catch (Exception e) { LOG.error("上传文件发生异常: {}", e.getMessage()); }
} LOG.info("上传OFD成功 - 总文件数: {},耗时: {}s", pdfFiles.size(), (DateUtil.current() - countStart) / 1000.0);
}
@Override boolean exist(PathUpLoadMessage message) { return Boolean.FALSE; } }
|