|
@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
import lombok.Data;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.net.URL;
|
|
|
|
|
|
@Data
|
|
|
@TableName("file_update_info")
|
|
@@ -26,10 +27,40 @@ public class FileUpdateInfo {
|
|
|
return fileName.substring(0, fileName.lastIndexOf('.')) + "-" + version + fileName.substring(fileName.lastIndexOf('.'));
|
|
|
}
|
|
|
|
|
|
- // 获取文件完整路径(基于当前服务运行目录)
|
|
|
+ // 获取文件完整路径(基于 JAR 文件所在的目录)
|
|
|
public String getFilePath() {
|
|
|
- // 获取当前服务运行目录
|
|
|
- String currentDir = System.getProperty("user.dir");
|
|
|
- return currentDir + File.separator + fileName;
|
|
|
+ return getServiceDir() + File.separator + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 动态获取 JAR 文件所在的目录
|
|
|
+ public static String getServiceDir() {
|
|
|
+ try {
|
|
|
+ // 获取当前类的 ClassLoader
|
|
|
+ ClassLoader classLoader = FileUpdateInfo.class.getClassLoader();
|
|
|
+ // 获取当前类的资源路径(例如 JAR 文件路径)
|
|
|
+ URL url = classLoader.getResource(FileUpdateInfo.class.getName().replace('.', '/') + ".class");
|
|
|
+ if (url != null && "jar".equals(url.getProtocol())) {
|
|
|
+ // 从 JAR 文件中获取路径
|
|
|
+ String jarPath = url.getPath();
|
|
|
+ // 去掉 JAR 文件内部路径部分
|
|
|
+ int bangIndex = jarPath.indexOf("!/");
|
|
|
+ if (bangIndex > 0) {
|
|
|
+ jarPath = jarPath.substring(0, bangIndex);
|
|
|
+ }
|
|
|
+ // 去掉 "file:" 前缀
|
|
|
+ if (jarPath.startsWith("file:")) {
|
|
|
+ jarPath = jarPath.substring(5);
|
|
|
+ }
|
|
|
+ // 获取 JAR 文件所在的目录
|
|
|
+ return new java.io.File(jarPath).getParent();
|
|
|
+ } else if (url != null && "file".equals(url.getProtocol())) {
|
|
|
+ // 如果是直接运行的文件路径
|
|
|
+ String filePath = url.getPath();
|
|
|
+ return new java.io.File(filePath).getParent();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return System.getProperty("user.dir"); // 如果无法获取 JAR 路径,退回到当前工作目录
|
|
|
}
|
|
|
}
|