zhuang 1 månad sedan
förälder
incheckning
324b279c21

+ 50 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/utils/SystemDownloadDir.java

@@ -0,0 +1,50 @@
+package com.rongwei.bscommon.sys.utils;
+
+import java.io.File;
+
+public class SystemDownloadDir {
+    // 获取系统默认的下载目录
+    public static String getSystemDownloadPath() {
+        String userHome = System.getProperty("user.home");
+        String os = System.getProperty("os.name").toLowerCase();
+        String downloadDir;
+
+        if (os.contains("win")) {
+            // Windows 系统:C:\Users\<用户名>\Downloads
+            downloadDir = userHome + "\\Downloads";
+        } else if (os.contains("mac")) {
+            // macOS 系统:/Users/<用户名>/Downloads
+            downloadDir = userHome + "/Downloads";
+        } else {
+            // Linux 系统:/home/<用户名>/Downloads 或 /home/<用户名>/下载
+            // 尝试读取 Linux 的 XDG 用户目录配置
+            String xdgDownloadDir = getLinuxDownloadDir(userHome);
+            downloadDir = (xdgDownloadDir != null) ? xdgDownloadDir : userHome + "/Downloads";
+        }
+
+        return downloadDir;
+    }
+
+    // Linux 系统:通过解析 ~/.config/user-dirs.dirs 获取下载目录
+    private static String getLinuxDownloadDir(String userHome) {
+        try {
+            File configFile = new File(userHome, ".config/user-dirs.dirs");
+            if (configFile.exists()) {
+                // 读取文件内容,查找 XDG_DOWNLOAD_DIR
+                String content = new String(java.nio.file.Files.readAllBytes(configFile.toPath()));
+                for (String line : content.split("\n")) {
+                    if (line.startsWith("XDG_DOWNLOAD_DIR")) {
+                        String path = line.split("=")[1]
+                                .replace("\"", "")
+                                .replace("$HOME", userHome)
+                                .trim();
+                        return path;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+}