public class PathUtil {
//获取项目的根路径
public final static String CLASS_PATH;
static {
//获取的是classpath路径,适用于读取resources下资源
CLASS_PATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
}
/**
* 项目根目录
*/
public static String getRootPath() {
return rootPath("");
}
/**
* 项目相对路径
**/
public static String getRelativePath() {
return System.getProperty("user.dir");
}
/**
* 获取classpath/resources资源路径
**/
public static String getResourcePath() throws FileNotFoundException {
return ResourceUtils.getURL("classpath:").getPath();
}
/**
* 自定义追加路径
*/
public static String getRootPath(String path) {
return rootPath("/" + path);
}
/**
* 私有处理方法
*/
public static String rootPath(String path) {
String rootPath = "";
//windows下
if ("\\".equals(File.separator)) {
rootPath = CLASS_PATH + path;
rootPath = rootPath.replaceAll("/", "\\\\");
if (rootPath.charAt(0) == '\\') {
rootPath = rootPath.substring(1);
}
}
//linux下
if ("/".equals(File.separator)) {
rootPath = CLASS_PATH + path;
rootPath = rootPath.replaceAll("\\\\", "/");
}
return rootPath;
}
}
PathUtil
最新推荐文章于 2024-05-16 20:42:16 发布