PathUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.net.URLDecoder;
  6. /**
  7. * 路径工具
  8. *
  9. * @author WANGWEI
  10. */
  11. public class PathUtil {
  12. /**
  13. * 获取标准路径
  14. *
  15. * @author WANGWEI
  16. * @param path
  17. * @return
  18. */
  19. public static String getCanonicalPath(String path) {
  20. path = path.replaceAll("\\\\+", "/");
  21. path = path.replaceAll("/+", "/");
  22. return path;
  23. }
  24. /**
  25. * 以"/"开头
  26. *
  27. * @author WANGWEI
  28. * @param path
  29. * @return
  30. */
  31. public static String startsWithSeparator(String path) {
  32. path = getCanonicalPath(path);
  33. if (path.startsWith("/")) {
  34. return path;
  35. }
  36. return "/" + path;
  37. }
  38. /**
  39. * 不以"/"开头
  40. *
  41. * @author WANGWEI
  42. * @param path
  43. * @return
  44. */
  45. public static String startsWithoutSeparator(String path) {
  46. path = getCanonicalPath(path);
  47. if (path.startsWith("/")) {
  48. return path.substring(1);
  49. }
  50. return path;
  51. }
  52. /**
  53. * 以"/"结尾
  54. *
  55. * @author WANGWEI
  56. * @param path
  57. * @return
  58. */
  59. public static String endsWithSeparator(String path) {
  60. path = getCanonicalPath(path);
  61. if (path.endsWith("/")) {
  62. return path;
  63. }
  64. return path + "/";
  65. }
  66. /**
  67. * 不以"/"结尾
  68. *
  69. * @author WANGWEI
  70. * @param path
  71. * @return
  72. */
  73. public static String endsWithoutSeparator(String path) {
  74. path = getCanonicalPath(path);
  75. if (path.endsWith("/")) {
  76. return path.substring(0, path.length() - 1);
  77. }
  78. return path;
  79. }
  80. /**
  81. * 获取路径
  82. *
  83. * @author WANGWEI
  84. * @param file
  85. * @return
  86. */
  87. public static String getCanonicalPath(File file) {
  88. try {
  89. return file.getCanonicalPath();
  90. } catch (IOException e) {
  91. throw new RuntimeException("Fail to get canonical path.", e);
  92. }
  93. }
  94. /**
  95. * 获取当前路径
  96. *
  97. * @author WANGWEI
  98. * @return
  99. * @throws IOException
  100. */
  101. public static String currentPath() {
  102. File directory = new File(". ");
  103. return getCanonicalPath(directory);
  104. }
  105. /**
  106. * 获取资源路径
  107. *
  108. * @author WANGWEI
  109. * @param resourceName
  110. * @return
  111. */
  112. public static String getResoucePath(String resourceName) {
  113. try {
  114. ClassLoader classLoader = PathUtil.class.getClassLoader();
  115. URL url = classLoader.getResource(resourceName);
  116. if (null != url) {
  117. String path = URLDecoder.decode(url.getPath(), "UTF-8");
  118. return path;
  119. } else {
  120. return null;
  121. }
  122. } catch (Exception e) {
  123. throw new RuntimeException(e);
  124. }
  125. }
  126. /**
  127. * 获取windows盘符
  128. *
  129. * @author WANGWEI
  130. * @param path
  131. * @return
  132. */
  133. public static String getDrive(String path) {
  134. if (path.matches("[a-zA-Z]:[\\\\/].*")) {
  135. return path.substring(0, 2);
  136. } else {
  137. throw new RuntimeException("Path is not a windows path.");
  138. }
  139. }
  140. /**
  141. * 拼接路径
  142. *
  143. * @author WANGWEI
  144. * @param fragments
  145. * @return
  146. */
  147. public static String joinUrl(String... fragments) {
  148. StringBuilder sb = new StringBuilder();
  149. for (int i = 0; i < fragments.length; i++) {
  150. String cur = fragments[i].trim();
  151. if (cur.endsWith("/")) {
  152. cur = cur.substring(0, cur.length() - 1);
  153. }
  154. if (0 == i) {
  155. sb.append(cur);
  156. } else if (cur.startsWith("/")) {
  157. sb.append(cur);
  158. } else {
  159. sb.append("/").append(cur);
  160. }
  161. }
  162. return sb.toString();
  163. }
  164. }