|
@@ -1,16 +1,66 @@
|
|
|
package com.example.cgnstohdf.service.processor.impl;
|
|
|
|
|
|
import com.example.cgnstohdf.service.processor.FileProcessor;
|
|
|
+import com.example.cgnstohdf.utils.ExeFileUtils;
|
|
|
+import com.example.cgnstohdf.utils.FileUtils;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Paths;
|
|
|
+import java.io.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
-public class CgnsFileProcessor {
|
|
|
-// @Override
|
|
|
-// public String processFile(String filePath) throws Exception {
|
|
|
-// // 读取文本文件内容
|
|
|
-// return new String(Files.readAllBytes(Paths.get(filePath)));
|
|
|
-// }
|
|
|
+public class CgnsFileProcessor implements FileProcessor{
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> processFile(String filePath) throws Exception {
|
|
|
+ // 读取文本文件内容
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ // 1. 复制exe到临时目录并获取JSON文件路径
|
|
|
+ String outputFile = FileUtils.getTempJsonFilePath();
|
|
|
+ System.out.println("filePath" + filePath + "outputFile" + outputFile);
|
|
|
+// File exeFile = FileUtils.copyExeFromResources("pythonScript/cgns_converter.exe");
|
|
|
+
|
|
|
+ ClassPathResource resource = new ClassPathResource("pythonScript/cgns_converter.exe");
|
|
|
+ File exeFile = resource.getFile();
|
|
|
+
|
|
|
+ // 检查文件是否存在且可执行
|
|
|
+ if (!exeFile.exists() || !exeFile.canExecute()) {
|
|
|
+ throw new RuntimeException("EXE文件不存在或不可执行");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 执行命令
|
|
|
+
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(
|
|
|
+ exeFile.getAbsolutePath(),
|
|
|
+ filePath,
|
|
|
+ outputFile
|
|
|
+ );
|
|
|
+ processBuilder.directory(exeFile.getParentFile());
|
|
|
+ processBuilder.redirectErrorStream(true);
|
|
|
+
|
|
|
+ Process process = processBuilder.start();
|
|
|
+ try (var reader = process.inputReader()) {
|
|
|
+ reader.lines().forEach(System.out::println); // 打印执行日志
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 检查执行状态
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ if (exitCode != 0) {
|
|
|
+ throw new RuntimeException("EXE执行失败,错误码: " + exitCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 读取JSON结果
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ Map<String, Object> jsonData = mapper.readValue(new File(outputFile), Map.class);
|
|
|
+ result.put("data", jsonData);
|
|
|
+
|
|
|
+ // 5. 清理临时文件(可选)
|
|
|
+ exeFile.deleteOnExit();
|
|
|
+ new File(outputFile).deleteOnExit();
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
}
|