From 8ede6183253248e497d391a0902bb5d41181b3bf Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期二, 12 八月 2025 20:39:18 +0800
Subject: [PATCH] 文件处理
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java | 94 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 94 insertions(+), 0 deletions(-)
diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
index faf428c..dbe2c10 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/gkw/programnode/service/MachineFileService.java
@@ -3,12 +3,28 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizServiceImpl;
+import org.springblade.core.tool.utils.Charsets;
import org.springblade.core.tool.utils.Func;
+import org.springblade.mdm.basesetting.machine.MachineService;
+import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.gkw.programnode.entity.MachineFile;
import org.springblade.mdm.gkw.programnode.mapper.MachineFileMapper;
+import org.springblade.mdm.utils.FileContentUtil;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
/**
@@ -20,6 +36,84 @@
@Service
@AllArgsConstructor
public class MachineFileService extends BizServiceImpl<MachineFileMapper, MachineFile> {
+ private final MachineService machineService;
+ /**
+ * 妫�娴嬫枃浠舵槸鍚﹀瓨鍦�
+ * @param name 鏂囦欢鍚�
+ * @param md5 鏂囦欢md5
+ * @param machineCode 鎵�灞炴満搴�
+ * @return 鏄惁瀛樺湪浜庡簱鍐�
+ */
+ public boolean fileExists(String name, String md5,String machineCode) {
+ return this.lambdaQuery().eq(MachineFile::getName, name).eq(MachineFile::getMd5, md5).eq(MachineFile::getMachineCode, machineCode).count()>0;
+ }
+ @Transactional(readOnly = true)
+ public String getMachineFileContent(Long id) {
+ MachineFile machineFile = getById(id);
+ Machine machine = machineService.getByCode(machineFile.getMachineCode());
+ String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
+
+ String content;
+
+ Path filePath = Paths.get(filePathStr);
+ if(!filePath.toFile().exists()){
+ return StringUtils.EMPTY;
+ }
+ try (InputStream inputStream = Files.newInputStream(filePath)) {
+ // 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+ content = FileContentUtil.getContentFromStream(inputStream);
+ } catch (IOException e) {
+ log.error("璇诲彇鏂囦欢md5澶辫触",e);
+ return StringUtils.EMPTY;
+ }
+
+ return content;
+ }
+
+ /**
+ * 鑾峰彇鍩烘湰璺緞
+ * @param machine
+ * @param dirType
+ * @return
+ */
+ public static String getBasePath(Machine machine,String dirType){
+ String dirPath;
+ switch (dirType) {
+ case MachineFile.DIR_TYPE_REC:
+ dirPath = machine.getProgReceiveDir();
+ break;
+ case MachineFile.DIR_TYPE_SEND:
+ dirPath = machine.getProgSendDir();
+ break;
+ case MachineFile.DIR_TYPE_TEMP:
+ dirPath = machine.getProgTempDir();
+ break;
+ default:
+ log.warn("鐩綍绫诲瀷涓嶅尮閰�:{}",dirType);
+ return null;//涓嶇鍚堜换浣曠被鍨嬶紝鐩存帴閫�鍑�
+ }
+ return dirPath;
+ }
+
+ @Transactional
+ public void saveFileConent(Long id, String content) throws IOException {
+ MachineFile machineFile = getById(id);
+ Machine machine = machineService.getByCode(machineFile.getMachineCode());
+
+ String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
+ Path filePath = Paths.get(filePathStr);
+ String charsetName = "UTF-8";
+ try (InputStream inputStream = Files.newInputStream(filePath)) {
+ // 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+ charsetName = FileContentUtil.detectFromInputStream(inputStream);
+ } catch (IOException e) {
+ log.error("璇诲彇鏂囦欢缂栫爜澶辫触",e);
+ throw new ServiceException("鑾峰彇鏂囦欢缂栫爜澶辫触");
+ }
+
+ FileUtils.writeStringToFile(filePath.toFile(),content,charsetName);
+
+ }
}
--
Gitblit v1.9.3