相对路径与绝对路径
1、相对路径,不以"/"开头
2、绝对路径,以"/"开头
3、如何写绝对路径:
在链接、表单提交、重定向时,绝对路径从应用名开始,而转发从应用名之后开始。
String request.getContextPath();
href="${pageContext.request.contextPath}/css/book.css"
${pageContext.request.contextPath}:得到webapp根目录即WebRoot
读取jar包外面的文件:
new FileInputStream(System.getProperty("user.dir")+"\\config\\db.properties");
在WEB工程中,获取src下目录的路径
如在src下有ccapcfg目录,则部署后会出现在WEB-INF/classes/ccapcfg路径,在java类中获取此路径的方法(特别适用于Websphere):
MyBean bean = new MyBean();//随便创建一个实例
StringBuilder sb = new StringBuilder();
sb.append(bean.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
sb.appren("ccapcfg/");
sb.append(fileName);//实际要读取的文件名
sb.toString();//得到完整路径
在打成可运行的jar程序获取当前jar文件所在路径:
path = URLDecoder.decode(LoadCity.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8");