java获取当前路径有哪几种方法呢?
下文笔者讲述使用java代码获取路径的方法分享,如下所示
例:
jsp中获取路径的示例
实现思路:
使用ystem.getProperty()
使用File()对象获取
获取路径的语法说明
方式1:
使用System.getProperty()函数获取当前路径:
//打印当前路径
System.out.println(System.getProperty("user.dir"));
方式2:
使用File提供的函数获取当前路径:
//设置File获取当前文件夹
File directory = new File("");
try{
System.out.println(directory.getCanonicalPath());//获取标准的路径
System.out.println(directory.getAbsolutePath());//获取绝对路径
}catch(Exceptin e){
}
注意事项:
File directory = new File("maomao");
directory.getCanonicalPath(); //返回C:/test/maomao
directory.getAbsolutePath(); //返回C:/test/maomao
direcotry.getPath(); //返回maomao
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(); //返回..
System.getProperty()参数大全
| 参数信息 | 返回值 |
| # java.version | Java Runtime Environment version |
| # java.vendor | Java Runtime Environment vendor |
| # java.vendor.url | Java vendor URL |
| # java.home | Java installation directory |
| # java.vm.specification.version | Java Virtual Machine specification version |
| # java.vm.specification.vendor | Java Virtual Machine specification vendor |
| # java.vm.specification.name | Java Virtual Machine specification name |
| # java.vm.version | Java Virtual Machine implementation version |
| # java.vm.vendor | Java Virtual Machine implementation vendor |
| # java.vm.name | Java Virtual Machine implementation name |
| # java.specification.version | Java Runtime Environment specification version |
| # java.specification.vendor | Java Runtime Environment specification vendor |
| # java.specification.name | Java Runtime Environment specification name |
| # java.class.version | Java class format version number |
| # java.class.path | Java class path |
| # java.library.path | list of paths to search when loading libraries |
| # java.io.tmpdir | Default temp file path |
| # java.compiler | Name of JIT compiler to use |
| # java.ext.dirs | Path of extension directory or directories |
| # os.name | Operating system name |
| # os.arch | Operating system architecture |
| # os.version | Operating system version |
| # File.separator | File separator ("/" on UNIX) |
| # path.separator | Path separator (":" on UNIX) |
| # line.separator | Line separator ("/n" on UNIX) |
| # user.name | User’s account name |
| # user.home | User’s home directory |
| # user.dir | User’s current working directory |
jsp中获取路径的示例
例:
工程名为Java265
//返回当前路径全名称
request.getRequestURI()
/Java265/test.jsp
//获取工程名
request.getContextPath()
/Java265
//当前页面所在目录下全名称
request.getServletPath()
当页面在jsp目录下
/Java265/jsp/test.jsp
页面所在服务器的全路径
application.getRealPath("页面.jsp")
D:/test/webapps/Java265/test.jsp
页面所在服务器的绝对路径
absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
D:/test/webapps/Java265
类中取得路径
类的绝对路径:Class.class.getClass().getResource("/").getPath()
/D:/TEST/WebRoot/WEB-INF/classes/pack/
获取工程的路径
System.getProperty("user.dir")
D:/TEST
Servlet中取得路径
获取工程目录:request.getSession().getServletContext().getRealPath("")
参数中可放入包名
E:/Tomcat/webapps/Java265
获取浏览器地址栏地址:request.getRequestURL()
http://localhost:8080/Java265/test
获取相对地址:request.getRequestURI()
/Java265/test
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


