package com.canrd.webmagic.common.directory; import com.canrd.webmagic.common.constant.Constant; /** * @author xms */ public class Path { /** * 获取程序启动路径 * * @param cls,建议直接传 ServiceApplication.class * @return 应用启动路径 */ public static String getAppPath(Class cls) { String path = cls.getResource(Constant.SLASH_MARK_CHARACTER).getPath(); String os = System.getProperty("os.name").toLowerCase(); final String OSWINDOW = "windows"; if (os.indexOf(OSWINDOW) != -1 && path.length() > 1) { //windows路径样例: /D:/work/code/shop-services/online-shop-oss/target/classes/ return path.substring(1); } //linux 路径样例 file:/opt/target/online-shop-oss-SNAPSHOT.jar!/BOOT-INF/classes!/"; final String FILE = "file:"; if (path.indexOf(FILE) != -1) { path = path.substring(FILE.length()); } int pos = -1; if ((pos = path.indexOf(Constant.EXCLAMATION_MARK_CHARACTER)) != -1) { if (-1 != (pos = path.lastIndexOf(Constant.SLASH_MARK_CHARACTER, pos))) { path = path.substring(0, pos + 1); } } return path; } }