PathUtil.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.sooka.common.utils;
  2. import java.io.File;
  3. /**
  4. * new File("..\path\abc.txt") 中的三个方法获取路径的方法
  5. * 1: getPath() 获取相对路径,例如 ..\path\abc.txt
  6. * 2: getAbslutlyPath() 获取绝对路径,但可能包含 ".." 或 "." 字符,例如 D:\otherPath\..\path\abc.txt
  7. * 3: getCanonicalPath() 获取绝对路径,但不包含 ".." 或 "." 字符,例如 D:\path\abc.txt
  8. */
  9. public class PathUtil {
  10. private static String webRootPath;
  11. private static String rootClassPath;
  12. public static String getPath(Class clazz) {
  13. String path = clazz.getResource("").getPath();
  14. return new File(path).getAbsolutePath();
  15. }
  16. public static String getPath(Object object) {
  17. String path = object.getClass().getResource("").getPath();
  18. return new File(path).getAbsolutePath();
  19. }
  20. // 注意:命令行返回的是命令行所在的当前路径
  21. public static String getRootClassPath() {
  22. if (rootClassPath == null) {
  23. try {
  24. String path = PathUtil.class.getClassLoader().getResource("").toURI().getPath();
  25. rootClassPath = new File(path).getAbsolutePath();
  26. }
  27. catch (Exception e) {
  28. String path = PathUtil.class.getClassLoader().getResource("").getPath();
  29. rootClassPath = new File(path).getAbsolutePath();
  30. }
  31. }
  32. return rootClassPath;
  33. }
  34. public static void setRootClassPath(String rootClassPath) {
  35. PathUtil.rootClassPath = rootClassPath;
  36. }
  37. public static String getPackagePath(Object object) {
  38. Package p = object.getClass().getPackage();
  39. return p != null ? p.getName().replaceAll("\\.", "/") : "";
  40. }
  41. public static File getFileFromJar(String file) {
  42. throw new RuntimeException("Not finish. Do not use this method.");
  43. }
  44. public static String getWebRootPath() {
  45. if (webRootPath == null) {
  46. webRootPath = detectWebRootPath();
  47. }
  48. return webRootPath;
  49. }
  50. public static void setWebRootPath(String webRootPath) {
  51. if (webRootPath == null) {
  52. return ;
  53. }
  54. if (webRootPath.endsWith(File.separator)) {
  55. webRootPath = webRootPath.substring(0, webRootPath.length() - 1);
  56. }
  57. PathUtil.webRootPath = webRootPath;
  58. }
  59. // 注意:命令行返回的是命令行所在路径的上层的上层路径
  60. private static String detectWebRootPath() {
  61. try {
  62. String path = PathUtil.class.getResource("/").toURI().getPath();
  63. return new File(path).getParentFile().getParentFile().getCanonicalPath();
  64. } catch (Exception e) {
  65. throw new RuntimeException(e);
  66. }
  67. }
  68. public static boolean isAbsolutelyPath(String path) {
  69. return path.startsWith("/") || path.indexOf(":") == 1;
  70. }
  71. /*
  72. private static String detectWebRootPath() {
  73. try {
  74. String path = PathKit.class.getResource("/").getFile();
  75. return new File(path).getParentFile().getParentFile().getCanonicalPath();
  76. } catch (IOException e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. */
  81. }