跳至正文
-
收藏我们的新闻资讯,不错过任何精彩内容。
很文博客 很文博客 很文博客

生活点滴 贵在分享

很文博客 很文博客 很文博客

生活点滴 贵在分享

  • 首页
  • 站务联系
  • 首页
  • 站务联系
关

搜索

软件应用

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

2018年4月20日
0

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;

标签:

classpathJavarootwebapp
作者

很文博客

关注我
其他文章
上一个

Apple iOS developer开发者账号申请

下一个

ZooKeeper的安装与部署

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

近期浏览最多文章

  • 作品观看平台+科学上网工具(不定期更新……) - 5,816 浏览
  • 「博多彩叶」出道作品番号及封面,「博多彩叶」个人简介 - 2,261 浏览
  • 「河北彩伽」最新作品SNOS-056介绍及封面预览 - 2,086 浏览
  • 森日向子(Mori-Hinako)最新作品MIAB-021介绍及封面预览 - 1,928 浏览
  • 「河北彩伽」最新作品SNOS-003介绍及封面预览 - 1,779 浏览

热门标签

Julia miru MySQL 三上悠亚 三上悠亜 习惯 伊藤舞雪 传奇故事 动物故事 励志名言 励志故事 危机故事 哲学哲理 哲理小语 哲理故事 寓言故事 小花のん 小花暖 希腊寓言故事 帝王故事 心情说说 惊险故事 战争故事 明里つむぎ 明里䌷 松本いちか 枫富爱 桃乃木かな 桃乃木香奈 桜空もも 梦乃あいか 樱空桃 欢迎词 河北彩花 波多野结衣 生日祝福语 生活小语 祝福短信 祝福语 神宫寺ナオ 神宫寺奈绪 童话故事 美谷朱里 自然人文 藤森里穂
Copyright 2026 — 很文博客. All rights reserved. Sitemap |站务联系 |备用网址