邮件API
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.mini.capi.config.component;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
@Component
|
||||
public class AppPathInfo {
|
||||
@@ -13,14 +15,29 @@ public class AppPathInfo {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
|
||||
// 获取 Jar 包所在目录(仅 Jar 运行时有效;IDE 中返回 classes 目录父级)
|
||||
public String getJarDir() throws URISyntaxException {
|
||||
URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
File jarFile = new File(jarUrl.toURI());
|
||||
return jarFile.getParentFile().getAbsolutePath();
|
||||
public String getJarDir() {
|
||||
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
String path = null;
|
||||
try {
|
||||
URLConnection connection = url.openConnection();
|
||||
if (connection instanceof JarURLConnection) {
|
||||
// 处理Jar包环境:获取Jar文件路径并截取目录
|
||||
JarURLConnection jarConn = (JarURLConnection) connection;
|
||||
String jarFilePath = jarConn.getJarFile().getName(); // 获取Jar文件完整路径(如:/xxx/xxx/xxx.jar)
|
||||
// 截取Jar文件所在目录(去掉文件名部分)
|
||||
path = jarFilePath.substring(0, jarFilePath.lastIndexOf(File.separator));
|
||||
} else {
|
||||
// 处理本地开发环境(文件夹形式运行)
|
||||
path = new File(url.toURI()).getParentFile().getAbsolutePath();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// 兜底方案:获取当前工作目录
|
||||
path = System.getProperty("user.dir");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// 获取类路径下的资源目录(IDE 中是 target/classes,Jar 中是 jar 内部路径)
|
||||
public String getResourceDir() {
|
||||
URL resourceUrl = getClass().getClassLoader().getResource("");
|
||||
if (resourceUrl != null) {
|
||||
|
||||
Reference in New Issue
Block a user