java之路径问题

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


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


三、System.getProperty("user.dir")
获取项目的根路径,在web项目中会由于web容器的不一样而不一样,在java项目用this


四、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>xml

相关文章
相关标签/搜索