java之路径问题

1、this.class.getClassLoader().getResource("路径").getPath();
路径参数不能以“/”开头,获取classes目录下文件的路径


2、this.class.getResource("路径").getPath();
路径参数以“/”开头,则在classes目录下找,不以“/”开头则在当前类的包目录下找


3、System.getProperty("user.dir")
获取项目的根路径,在web项目中会因为web容器的不同而不同,在java项目用


4、getServletContext().getRealPath("路径")
获取web项目文件的硬盘路径,路径参数以“/”开关
WebLogic在发布WAR格式的应用时,servletContext.getRealPath的默认返回Null,解决方法
在web项目的WEB-INF/weblogic.xml文件(需要手动创建)中添加配置
<container-descriptor>   
  <show-archived-real-path-enabled>true</show-archived-real-path-enabled> 
</container-descriptor>

posted on 2014-06-30 17:09  穆穆  阅读(177)  评论(0编辑  收藏  举报