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ì)有所幫助。