WANG 6 年之前
父节点
当前提交
3528b69627
共有 1 个文件被更改,包括 63 次插入4 次删除
  1. 63 4
      src/main/java/cn/com/qmth/examcloud/commons/util/PathUtil.java

+ 63 - 4
src/main/java/cn/com/qmth/examcloud/commons/util/PathUtil.java

@@ -5,8 +5,6 @@ import java.io.IOException;
 import java.net.URL;
 import java.net.URLDecoder;
 
-import org.apache.commons.lang3.StringUtils;
-
 /**
  * 路径工具
  *
@@ -22,8 +20,69 @@ public class PathUtil {
 	 * @return
 	 */
 	public static String getCanonicalPath(String path) {
-		path = path.replaceAll("\\+", "/").replaceAll("/+", "/").replaceAll("/\\./", "/");
-		return StringUtils.replace(path, "/", File.separator);
+		path = path.replaceAll("\\\\+", "/");
+		path = path.replaceAll("/+", "/");
+		return path;
+	}
+
+	/**
+	 * 以"/"开头
+	 *
+	 * @author WANGWEI
+	 * @param path
+	 * @return
+	 */
+	public static String startsWithSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.startsWith("/")) {
+			return path;
+		}
+		return "/" + path;
+	}
+
+	/**
+	 * 不以"/"开头
+	 *
+	 * @author WANGWEI
+	 * @param path
+	 * @return
+	 */
+	public static String startsWithoutSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.startsWith("/")) {
+			return path.substring(1);
+		}
+		return path;
+	}
+
+	/**
+	 * 以"/"结尾
+	 *
+	 * @author WANGWEI
+	 * @param path
+	 * @return
+	 */
+	public static String endsWithSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.endsWith("/")) {
+			return path;
+		}
+		return path + "/";
+	}
+
+	/**
+	 * 不以"/"结尾
+	 *
+	 * @author WANGWEI
+	 * @param path
+	 * @return
+	 */
+	public static String endsWithoutSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.endsWith("/")) {
+			return path.substring(0, path.length() - 1);
+		}
+		return path;
 	}
 
 	/**