文件路径小结

一、java工程:
    System.out.println("==============");
    System.out.println("当前classPath路劲:" + Thread.currentThread().getContextClassLoader().getResource("").getPath());
    System.out.println("当前项目路劲:" + System.getProperty("user.dir"));
    System.out.println("当前类classPath路劲(不包含自己):" + FilePath.class.getResource("").getPath());
    System.out.println("配置文件路径1:" + FilePath.class.getResource("/conf/data.txt").getPath());
    System.out.println("配置文件路径2:" + FilePath.class.getClassLoader().getResource("conf/data.txt").getPath());
    System.out.println("==============");
    ==============
    当前classPath路劲:/D:/workspace/workspace6/J2EEPro/myTools/WebRoot/WEB-INF/classes/
    当前项目路劲:D:\workspace\workspace6\J2EEPro\myTools
    当前类classPath路劲(不包含自己):/D:/workspace/workspace6/J2EEPro/myTools/WebRoot/WEB-INF/classes/com/cpkf/util/
    配置文件路径1:/D:/workspace/workspace6/J2EEPro/myTools/WebRoot/WEB-INF/classes/conf/data.txt
    配置文件路径2:/D:/workspace/workspace6/J2EEPro/myTools/WebRoot/WEB-INF/classes/conf/data.txt
    ==============

二、servlet:
    System.out.println("==========");
    System.out.println(request.getServletPath());
    System.out.println("工程硬盘路径:" + request.getSession().getServletContext().getRealPath(""));
    System.out.println("工程硬盘路径:" + getServletConfig().getServletContext().getRealPath("/"));
    System.out.println("拼接工程访问路径:" + request.getScheme() + "://"
        + request.getServerName() + ":" + request.getServerPort() + request.getContextPath());
    System.out.println("请求地址:" + request.getRequestURL());
    Properties properties = new Properties();
    properties.load(getClass().getResourceAsStream("/conf/datainfo.properties"));
    System.out.println(properties.get("htmlMax"));
    System.out.println("配置文件绝对路径:" + getClass().getResource("/conf/datainfo.properties").getPath());
    System.out.println("==========");
    ==========
    /getPath
    工程硬盘路径:D:\servers\tomcat-6.0.16\webapps\myTools
    工程硬盘路径:D:\servers\tomcat-6.0.16\webapps\myTools\
    拼接工程访问路径:http://localhost:8088/myTools
    请求地址:http://localhost:8088/myTools/getPath
    10000
    配置文件绝对路径:/D:/servers/tomcat-6.0.16/webapps/myTools/WEB-INF/classes/conf/datainfo.properties
    ==========

三、jsp:
    当前web应用绝对路径:< %=application.getRealPath("/") %>
    当前请求返回url路径:< %=request.getRequestURI() %>
    当前请求返回jsp绝对路径:< %=application.getRealPath(request.getRequestURI()) %>
    当前请求返回jsp上一层绝对路径:< %=new File(application.getRealPath(request.getRequestURI())).getParent() %>
    
    当前web应用绝对路径:D:\servers\tomcat-6.0.16\webapps\myTools\
    当前请求返回url路径:/myTools/web/pathJsp.jsp
    当前请求返回jsp绝对路径:D:\servers\tomcat-6.0.16\webapps\myTools\myTools\web\pathJsp.jsp
    当前请求返回jsp上一层绝对路径:D:\servers\tomcat-6.0.16\webapps\myTools\myTools\web

四、其他
如果是maven工程,配置文件不在src目录下,可在pom.xml文件中配置resources节点:
<resources>
    <resource> 
        <targetPath>META-INF/plexus</targetPath> 
        <filtering>false</filtering> 
        <directory>${basedir}/src/main/plexus</directory> 
        <includes> 
            <include>configuration.xml</include> 
        </includes> 
        <excludes> 
            <exclude>**/*.properties</exclude> 
        </excludes> 
    </resource>
</resources>

directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下,默认的directory是${basedir}/src/main/resources.
在工程中,直接获取流:
stream = this.getClass().getResourceAsStream("/configuration.xml");

你可能感兴趣的:(tomcat,maven,Web,xml,jsp)