J2EE常见路径处理
2016-04-29 11:15
323 查看
资源路径
File(path)
对于UNIX操作系统,绝对路径总是以”/”开头,相对路径没有前缀;对于Windows操作系统,绝对路径格式是”盘符:\\path”,Windows UNC绝对路径的的前缀是”\\\\”。
如果path是相对路径,文件的绝对路径部分从user.dir系统属性获取。比如如果user.dir的值是:c:/Users/workspace/projectDemo,那么File("path.properties")指向的文件是c:/Users/workspace/projectDemo/path.properties。
MyClass.class.getResource(path)
MyClass是你自己定义的类,假设MyClass加上包名的全称是"com.demo.MyClass"。path不以"/"开头时,则是相对从"com/demo/"目录下取资源,如果没有找到返回null;
path 以"/"开头时,则是从ClassPath根目录下获取,如果没有找到返回null;
ClassLoader.getResource(path)
path不以"/"开头,从ClassPath根目录下查找资源,如果没有找到返回null;path 以"/"开头时,返回null;
即MyClass.class.getResource("/") == ClassLoader().getResource("")。
WEB请求或文件路径
请求的URL:request.getRequestURL()。请求去除协议部分后的路径:request.getRequestURI(),比如请求为http://localhost:8080/J2EEProject/path?hello=world,返回的是"/J2EEProject/path"。
Web应用根目录的在硬盘上的真实路径:ServletContext.getRealPath(""),比如返回"c:\tomcat\webapps\J2EEProject"。如果是调用ServletContext.getRealPath("/"),则返回"c:\tomcat\webapps\J2EEProject\"。
文件路径:ServletContext.getRealPath("readme.txt"),如果文件在Web根目录真实存在,则返回"c:\tomcat\webapps\J2EEProject\readme.txt"。
相关文章推荐
- AS3自写类整理笔记 ClassLoader类第1/2页
- sql2005 附加数据库出错(错误号:5123)解决方法
- C#中File类的文件操作方法详解
- asp #include file 与 #include virtual 的区别小结第1/2页
- PHP file_exists问题杂谈
- IIS访问ASP页面时报错The requested resource is in use.的解决办法
- css美化input file按钮的代码方法
- 改变文件域的样式实现思路同时兼容ie、firefox
- How to Auto Include a Javascript File
- 清理SQL Server 2008日志文件Cannot shrink log file 2 的解决方案
- Locate a File Using a File Open Dialog Box
- Save a File Using a File Save Dialog Box
- 基于java file 文件操作operate file of java的应用
- php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
- PHP 得到根目录的 __FILE__ 常量
- 解析php dirname()与__FILE__常量的应用
- file_get_contents获取不到网页内容的解决方法
- sql2005 create file遇到操作系统错误5拒绝访问 错误1802
- Can’t open file:'[Table]mytable.MYI'
- js 获取、清空input type="file"的值示例代码