java 获取路径的一些问题

来源:互联网 发布:lolking软件 编辑:程序博客网 时间:2024/05/29 17:39

Java获取当前项目路径:

  • object.class.getResource()方法获得当前生成的class的绝对路径(此方法在jar包中无效,因为他获得的是生成的class的路径,返回的内容最后包含/)

 

[c-sharp] view plain copy
  1. //当前的类名就是:GetFilePath  
  2. public static String getFilePath(String fileName)  
  3.     {  
  4.         String path = GetFilePath.class.getResource("").toString();  
  5.   
  6.         if (path != null)  
  7.         {  
  8.             path = path.substring(5, path.indexOf("WEB-INF") + 8);//如果是windwos将5变成6  
  9.             //System.out.println("current path :" + path);  
  10.         }  
  11.         return (path + fileName);  
  12.     }  
 

  • getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)

例如 项目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/


[java] view plain copy
  1. public String getCurrentPath(){  
  2.        //取得根目录路径  
  3.        String rootPath=getClass().getResource("/").getFile().toString();  
  4.        //当前目录路径  
  5.        String currentPath1=getClass().getResource(".").getFile().toString();  
  6.        String currentPath2=getClass().getResource("").getFile().toString();  
  7.        //当前目录的上级目录路径  
  8.        String parentPath=getClass().getResource("../").getFile().toString();  
  9.          
  10.        return rootPath;         
  11.   
  12.    }  



  • 利用System.getProperty()函数获取当前路径,得到项目文件夹的根目录,不带/ 

例如 项目在 D:\workspace\testPorjject

者System.getProperty("user.dir") 返回 D:\workspace\testPorjject 

[java] view plain copy
  1. System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径   
 

  • 使用File提供的函数获取当前路径: 

[java] view plain copy
  1. File directory = new File("");//设定为当前文件夹   
  2. try{   
  3.     System.out.println(directory.getCanonicalPath());//获取标准的路径   
  4.     System.out.println(directory.getAbsolutePath());//获取绝对路径   
  5. }catch(Exceptin e){}   
 

  •  web 项目中 

 jsp中:

 

[java] view plain copy
  1. request.getContextPath()  
  2. request.getSession().getServletContext().getRealPath()     
 

 

servlet中:

[java] view plain copy
  1. this.getServletContext().getRealPath("/");  
  2. this.getServlet().getServletContext().getRealPath("/");  
 

输入流加载资源文件的3种方式:

          1.从classpath根目录下加载指定名称的文件:
          InputStream inputStream= this.getClass().getClassLoader().getResourceAsStream("diagrams/testVariables.bpmn");

          2.从当前包下加载指定名称的文件:
          InputStream inputStream= this.getClass().getResourceAsStream("testVariables.bpmn");

          3.从classpath根目录下加载指定名称的文件:
          InputStream inputStream= this.getClass().getResourceAsStream("/diagrams/testVariables.bpmn");


在js中的一些路径问题:

    context中没有配置path属性,所以你的工程文件就是在根目录下,相当于path="";
    即使你直接在浏览器中输入你的服务器ip就会到你的jsp页面,而不是tomcat的默认页面;所以你通过request.getContextPath()得到的字符串是为空的;它是获得虚目录的;如果你想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web。


参考servlet中的接口:
    request.getScheme();  //返回的协议名称,默认是http

    request.getServerName();//返回的是你浏览器中显示的主机名,你自己试一下就知道了

    getServerPort();//获取服务器端口号

    request.getContextPath();//应该是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串。如果项目为abc, <%=request.getContextPath()% > 将得到abc,服务器端的路径则会自动加上,<a href="XXXX.jsp"> 是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不过用的最多的还是request.getContextPath。
    在js文件中得到request.getContextPath()的值,不想在JSP中写太多的JavaScript代码:
         一种方法是用hidden:<input type=hidden name=contextPath value=<%= request.getContextPath()>
         再就是在本页面的js里面也是可以使用Scriptlet来赋值的:var a = '<%= request.getContextPath()>'      或者你赋值给一个hidden的控件都是可以的,然后js取,这样js可以不用写在jsp里。


    实际应用中,一般用来解决jsp测试和生产环境路径不同的问题: 
      <%
         String appContext = request.getContextPath();
         String basePath = request.getScheme()+"://"+request.getServerName()+":"+

               request.getServerPort() + appContext; 
      %>


     
 ${pageContext.request.contextPath}

<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><c:set var="ctx" value="${pageContext.request.contextPath}"/><link rel="stylesheet" rev="stylesheet" type="text/css" href="${ctx}/skin/default/css/default.css" media="all"/><script language="javascript" src="${ctx}/js/common.js"></script>


1 0