Ver código fonte

cgns格式文件处理,集成python

lichunyang 5 meses atrás
pai
commit
716c4042d4

+ 6 - 0
pom.xml

@@ -54,6 +54,12 @@
 			<version>2.6.1</version>
 		</dependency>
 
+<!--		<dependency>-->
+<!--			<groupId>com.fasterxml.jackson.core</groupId>-->
+<!--			<artifactId>jackson-databind</artifactId>-->
+<!--			<version>2.13.4.2</version>-->
+<!--		</dependency>-->
+
 	</dependencies>
 
 	<build>

+ 1 - 1
src/main/java/com/example/cgnstohdf/service/factory/FileProcessorFactory.java

@@ -23,7 +23,7 @@ public class FileProcessorFactory {
         processorMap.put("xyz", xyzFileProcessor);
 //        processorMap.put("plt", pltFileProcessor);
         processorMap.put("bdf", bdfFileProcessor);
-//        processorMap.put("cgns", cgnsFileProcessor);
+        processorMap.put("cgns", cgnsFileProcessor);
     }
 
     public FileProcessor getProcessor(String fileExtension) {

+ 58 - 8
src/main/java/com/example/cgnstohdf/service/processor/impl/CgnsFileProcessor.java

@@ -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;
+
+    }
 }

+ 33 - 0
src/main/java/com/example/cgnstohdf/utils/ExeFileUtils.java

@@ -0,0 +1,33 @@
+package com.example.cgnstohdf.utils;
+
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StreamUtils;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class ExeFileUtils {
+
+    /**
+     * 从 resources 复制可执行文件到临时目录
+     */
+    public static File copyExeFromResources(String resourcePath) throws Exception {
+        ClassPathResource resource = new ClassPathResource(resourcePath);
+        Path tempDir = Files.createTempDirectory("exe_temp");
+        File exeFile = new File(tempDir.toFile(), "cgnsJava.exe");
+
+        try (InputStream in = resource.getInputStream();
+             FileOutputStream out = new FileOutputStream(exeFile)) {
+            StreamUtils.copy(in, out);
+        }
+
+        // 设置可执行权限(仅限 Linux/macOS)
+        if (!System.getProperty("os.name").toLowerCase().contains("win")) {
+            exeFile.setExecutable(true);
+        }
+
+        return exeFile;
+    }
+}

+ 53 - 0
src/main/java/com/example/cgnstohdf/utils/FileUtils.java

@@ -1,5 +1,16 @@
 package com.example.cgnstohdf.utils;
 
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StreamUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
 /**
  * 提取后缀,文件格式
  */
@@ -15,4 +26,46 @@ public class FileUtils {
         }
         return fileName.substring(lastDotIndex + 1).toLowerCase();
     }
+
+    /**
+     * 从 resources 复制可执行文件到临时目录
+     */
+    public static File copyExeFromResources(String resourcePath) throws IOException {
+        ClassPathResource resource = new ClassPathResource(resourcePath);
+        Path tempDir = Files.createTempDirectory("exe_temp");
+        File exeFile = new File(tempDir.toFile(), "cgnsJava.exe");
+
+        try (InputStream in = resource.getInputStream();
+             FileOutputStream out = new FileOutputStream(exeFile)) {
+            StreamUtils.copy(in, out);
+        }
+
+        // 设置可执行权限(非Windows系统)
+        if (!System.getProperty("os.name").toLowerCase().contains("win")) {
+            exeFile.setExecutable(true);
+        }
+
+        return exeFile;
+    }
+
+    /**
+     * 获取临时目录下的JSON文件路径(自动创建父目录和空文件)
+     */
+    public static String getTempJsonFilePath() throws IOException {
+        String fileName = "threejs_ready_data.json";
+        String tempDir = System.getProperty("java.io.tmpdir");
+        Path filePath = Paths.get(tempDir, fileName);
+
+        // 确保父目录存在
+        if (!Files.exists(filePath.getParent())) {
+            Files.createDirectories(filePath.getParent());
+        }
+
+        // 如果文件不存在则创建空文件
+        if (!Files.exists(filePath)) {
+            Files.createFile(filePath);
+        }
+
+        return filePath.toString();
+    }
 }

BIN
src/main/resources/pythonScript/cgns_converter.exe