|
@@ -289,70 +289,52 @@ public class TargetIdentificationSubtaskDetailsController extends BaseController
|
|
|
public CommonResult<Void> openDir(String directory, String type) throws IOException {
|
|
|
String path = profile + "/task" + directory;
|
|
|
log.info("check dir: {}", path);
|
|
|
- if("1".equals(type)){
|
|
|
+ if (GraphicsEnvironment.isHeadless()) {
|
|
|
+ log.info("Headless 模式,改用命令行方式");
|
|
|
+ openFolderViaCommandLine(path);
|
|
|
+ } else {
|
|
|
+ log.info("使用Desktop 模式");
|
|
|
try {
|
|
|
Desktop desktop = Desktop.getDesktop();
|
|
|
- File dir = new File(path);
|
|
|
- if (dir.exists()) {
|
|
|
- desktop.open(dir);
|
|
|
+ if (desktop.isSupported(Desktop.Action.OPEN)) {
|
|
|
+ desktop.open(new File(path));
|
|
|
} else {
|
|
|
- System.out.println("文件夹不存在: " + path);
|
|
|
+ log.info("Desktop.OPEN 不支持,改用命令行方式");
|
|
|
+ openFolderViaCommandLine(path);
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("Desktop 失败:" + e.getMessage());
|
|
|
+ openFolderViaCommandLine(path);
|
|
|
}
|
|
|
}
|
|
|
+ return CommonResult.success();
|
|
|
+ }
|
|
|
|
|
|
- if("2".equals(type)){
|
|
|
- try {
|
|
|
- // 对于Ubuntu/GNOME桌面环境
|
|
|
- Process process = Runtime.getRuntime().exec(new String[]{"xdg-open", path});
|
|
|
-
|
|
|
- // 或者使用nautilus(文件管理器)
|
|
|
- // Process process = Runtime.getRuntime().exec(new String[]{"nautilus", path});
|
|
|
-
|
|
|
- int exitCode = process.waitFor();
|
|
|
- if (exitCode != 0) {
|
|
|
- System.out.println("打开文件夹失败,退出码: " + exitCode);
|
|
|
- }
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if("21".equals(type)){
|
|
|
- try {
|
|
|
-
|
|
|
- // 或者使用nautilus(文件管理器)
|
|
|
- Process process = Runtime.getRuntime().exec(new String[]{"nautilus", path});
|
|
|
-
|
|
|
- int exitCode = process.waitFor();
|
|
|
- if (exitCode != 0) {
|
|
|
- System.out.println("打开文件夹失败,退出码: " + exitCode);
|
|
|
- }
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ private void openFolderViaCommandLine(String path) {
|
|
|
+ String os = System.getProperty("os.name").toLowerCase();
|
|
|
+ log.info("Headless 模式,改用命令行方式,OS:" + os);
|
|
|
+ String command;
|
|
|
+
|
|
|
+ if (os.contains("linux")) {
|
|
|
+ command = "xdg-open";
|
|
|
+ } else if (os.contains("win")) {
|
|
|
+ command = "explorer";
|
|
|
+ } else if (os.contains("mac")) {
|
|
|
+ command = "open";
|
|
|
+ } else {
|
|
|
+ throw new UnsupportedOperationException("不支持的操作系统: " + os);
|
|
|
}
|
|
|
|
|
|
- if("3".equals(type)){
|
|
|
- try {
|
|
|
- ProcessBuilder pb = new ProcessBuilder("xdg-open", path);
|
|
|
- // 或者使用: ProcessBuilder pb = new ProcessBuilder("nautilus", path);
|
|
|
- pb.start();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- if("4".equals(type)){
|
|
|
- try {
|
|
|
- ProcessBuilder pb = new ProcessBuilder("nautilus", path);
|
|
|
- pb.start();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ try {
|
|
|
+ log.info("process start");
|
|
|
+ ProcessBuilder pb = new ProcessBuilder(command, path);
|
|
|
+ pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
|
|
|
+ pb.redirectError(ProcessBuilder.Redirect.DISCARD);
|
|
|
+ Process process = pb.start(); // 不调用 waitFor(),避免阻塞
|
|
|
+ log.info("process launched (non-blocking)");
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("Process error", e);
|
|
|
}
|
|
|
- return CommonResult.success();
|
|
|
}
|
|
|
|
|
|
@GetMapping("/getImgList")
|