Java 获取项目webapp, root,classpath各种路径工具类

  • A+
所属分类:软件应用教程

Java 获取webapp,root,classpath等各种路径主要是利用了System.getProperty(“user.dir”)获取你工程的绝对路径。然后再做相关操作。有的IDE如Intellij Idea在debug时路径会重定向到target下,然后目录和正常的目录略有差别,如果能拿到request在使用的过程中可以自己稍作修改,拼接。

  1. /**
  2.  * 工程路径获取
  3.  * Created by Water on 2017/7/28.
  4.  * Email:water471871679⊙gmail.com
  5.  */
  6. public class PathUtils {
  7.     /**
  8.      * 获取到classes目录
  9.      * @return path
  10.      */
  11.     public static String getClassPath(){
  12.         String systemName = System.getProperty("os.name");
  13.         //判断当前环境,如果是Windows 要截取路径的第一个 '/'
  14.         if(!StringUtils.isBlank(systemName) && systemName.indexOf("Windows") !=-1){
  15.             return PathUtils.class.getResource("/").getFile().toString().substring(1);
  16.         }else{
  17.             return PathUtils.class.getResource("/").getFile().toString();
  18.         }
  19.     }
  20.     /**
  21.      * 获取当前对象的路径
  22.      * @param object
  23.      * @return path
  24.      */
  25.     public static String getObjectPath(Object object){
  26.         return object.getClass().getResource(".").getFile().toString();
  27.     }
  28.     /**
  29.      * 获取到项目的路径
  30.      * @return path
  31.      */
  32.     public static String getProjectPath(){
  33.         return System.getProperty("user.dir");
  34.     }
  35.     /**
  36.      * 获取 root目录
  37.      * @return path
  38.      */
  39.     public static String getRootPath(){
  40.         return getWEB_INF().replace("WEB-INF/""");
  41.     }
  42.     /**
  43.      * 获取输出HTML目录
  44.      * @return
  45.      */
  46.     public static String getHTMLPath(){
  47.         return getFreePath() + "html/html/";
  48.     }
  49.     /**
  50.      * 获取输出FTL目录
  51.      * @return
  52.      */
  53.     public static String getFTLPath(){
  54.         return getFreePath() + "html/ftl/";
  55.     }
  56.     /**
  57.      * 获取 web-inf目录
  58.      * @return path
  59.      */
  60.     public static String getWEB_INF(){
  61.         return getClassPath().replace("classes/""");
  62.     }
  63.     /**
  64.      * 获取模版文件夹路径
  65.      * @return path
  66.      */
  67.     public static String getFreePath(){
  68.         return getWEB_INF() + "ftl/";
  69.     }
  70.     /**
  71.      * 文本换行,因为Linux  和 Windows 的换行符不一样
  72.      * @return
  73.      */
  74.     public static String nextLine(){
  75.         String nextLine = System.getProperty("line.separator");
  76.         return nextLine;
  77.     }
  78.     /**
  79.      * 获取images 路径
  80.      * @return
  81.      */
  82.     public static String getImages(){
  83.         return getRootPath() + "images/" ;
  84.     }
  85.     /**
  86.      * 获取sitemap 路径
  87.      * @return
  88.      */
  89.     public static String getSiteMapPath(){
  90.         return getRootPath() + "txt/sitemap" ;
  91.     }
  92.     /**
  93.      * 获取Txt 路径
  94.      * @return
  95.      */
  96.     public static String getTxt(){
  97.         return getRootPath() + "txt" ;
  98.     }
  99. }

其他

 

  1. ServletActionContext.getServletContext().getRealPath(“/”)

 

可以方便获取webapp目录,servletContext可以从HttpServletRequest中获取,如果是springmvc可以直接使用注解获得实例。

 

  1. @Autowired
  2. ServletContext servletContext;