jsp中获取当前目录的方法
本文实例讲述了jsp中获取当前目录的实现方法,分享给大家供大家参考。具体实现方法如下:
1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径2、使用File提供的函数获取当前路径:
Filedirectory=newFile("");//设定为当前文件夹
try{
System.out.println(directory.getCanonicalPath());//获取标准的路径
System.out.println(directory.getAbsolutePath());//获取绝对路径
}catch(Exceptine){}File.getCanonicalPath()和File.getAbsolutePath()大约只是对于newFile(".")和newFile("..")两种路径有所区别。
#对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
#对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在newFile()时设定的路径
#至于getPath()函数,得到的只是你在newFile()时设定的路径
比如当前的路径为C:test:
Filedirectory=newFile("abc");
directory.getCanonicalPath();//得到的是C:testabc
directory.getAbsolutePath();//得到的是C:testabc
direcotry.getPath();//得到的是abc
Filedirectory=newFile(".");
directory.getCanonicalPath();//得到的是C:test
directory.getAbsolutePath();//得到的是C:test.
direcotry.getPath();//得到的是.
Filedirectory=newFile("..");
directory.getCanonicalPath();//得到的是C:
directory.getAbsolutePath();//得到的是C:test..
direcotry.getPath();//得到的是..
获取JAVA程序当前的工作目录
Filefile=newFile("t.tmp");
Stringfullpath=file.getAbsolutePath();
①request.getRealPath:
方法:request.getRealPath("/")
得到的路径:C:ProgramFilesApacheSoftwareFoundationTomcat5.5webappsstrutsTest
方法:request.getRealPath(".")
得到的路径:C:ProgramFilesApacheSoftwareFoundationTomcat5.5webappsstrutsTest.
方法:request.getRealPath("")
得到的路径:C:ProgramFilesApacheSoftwareFoundationTomcat5.5webappsstrutsTest
方法:request.getRealPath("web.xml")
得到的路径:C:ProgramFilesApacheSoftwareFoundationTomcat5.5webappsstrutsTestweb.xml
②request.getParameter("");
ActionForm.getMyFile();
方法:Stringfilepath=request.getParameter("myFile");
得到的路径:D:VSS安装目录users.txt
方法:Stringfilepath=ActionForm.getMyFile();
得到的路径:D:VSS安装目录users.txt
希望本文所述对大家的jsp程序设计有所帮助。