jsp中獲取當(dāng)前目錄的方法

字號(hào):


    本文實(shí)例講述了jsp中獲取當(dāng)前目錄的實(shí)現(xiàn)方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
    1、利用System.getProperty()函數(shù)獲取當(dāng)前路徑:
    代碼如下:
    System.out.println(System.getProperty("user.dir"));//user.dir指定了當(dāng)前的路徑
    2、使用File提供的函數(shù)獲取當(dāng)前路徑:
    代碼如下:
    File directory = new File("");//設(shè)定為當(dāng)前文件夾
    try{
    System.out.println(directory.getCanonicalPath());//獲取標(biāo)準(zhǔn)的路徑
    System.out.println(directory.getAbsolutePath());//獲取絕對(duì)路徑
    }catch(Exceptin e){}
    File.getCanonicalPath()和File.getAbsolutePath()大約只是對(duì)于new File(".")和new File("..")兩種路徑有所區(qū)別。
    # 對(duì)于getCanonicalPath()函數(shù),“."就表示當(dāng)前的文件夾,而”..“則表示當(dāng)前文件夾的上一級(jí)文件夾
    # 對(duì)于getAbsolutePath()函數(shù),則不管”.”、“..”,返回當(dāng)前的路徑加上你在new File()時(shí)設(shè)定的路徑
    # 至于getPath()函數(shù),得到的只是你在new File()時(shí)設(shè)定的路徑
    比如當(dāng)前的路徑為 C:test :
    代碼如下:
    File directory = new File("abc");
    directory.getCanonicalPath(); //得到的是C:testabc
    directory.getAbsolutePath(); //得到的是C:testabc
    direcotry.getPath(); //得到的是abc
    File directory = new File(".");
    directory.getCanonicalPath(); //得到的是C:test
    directory.getAbsolutePath(); //得到的是C:test.
    direcotry.getPath(); //得到的是.
    File directory = new File("..");
    directory.getCanonicalPath(); //得到的是C:
    directory.getAbsolutePath(); //得到的是C:test..
    direcotry.getPath(); //得到的是..
    獲取 JAVA 程序當(dāng)前的工作目錄
    代碼如下:
    File file = new File("t.tmp");
    String fullpath = file.getAbsolutePath();
    ① request.getRealPath:
    方法:request.getRealPath("/")
    得到的路徑:C:Program FilesApache Software FoundationTomcat 5.5webappsstrutsTest
    方法:request.getRealPath(".")
    得到的路徑:C:Program FilesApache Software FoundationTomcat 5.5webappsstrutsTest.
    方法:request.getRealPath("")
    得到的路徑:C:Program FilesApache Software FoundationTomcat 5.5webappsstrutsTest
    方法:request.getRealPath("web.xml")
    得到的路徑:C:Program FilesApache Software FoundationTomcat 5.5webappsstrutsTestweb.xml
    ② request.getParameter("");
    ActionForm.getMyFile();
    方法:String filepath = request.getParameter("myFile");
    得到的路徑:D:VSS安裝目錄users.txt
    方法:String filepath = ActionForm.getMyFile();
    得到的路徑:D:VSS安裝目錄users.txt
    希望本文所述對(duì)大家的jsp程序設(shè)計(jì)有所幫助。