web应用加载资源文件

web应用加载资源文件


"."    表示相对路径中的当前路径。相对于java命令运行的目录。

结论:
用myeclipse工具开发java项目中, "."代表在java项目的根目录下MyEclipse工作空间/bin 目录开始
在web项目中,"."代表在tomcat/bin目录下开始,所以不能使用这种相对路径。

/   斜杠表示classpath的根目录

          在java项目下,classpath的根目录从bin目录开始
          在web项目下,classpath的根目录从WEB-INF/classes目录开始

1.给服务器使用。 / 表示在当前web应用的根目录下(WebRoot下面)
2.给浏览器使用。 / 表示在webapps的根目录下

使用web加载资源文件的推荐方式

方式1:String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");   读取,返回资源文件 的绝对路径。
方式2:InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); 读取,返回资源文件的输入流

在web项目Servlet中用“/”方式获取资源
Demo:
package com.cn.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年11月29日22:35:03
* Desc:
	"."		表示相对路径中的当前路径。相对于java命令运行的目录。
	结论:
		用myeclipse工具开发java项目中, "."代表在java项目的根目录下开始。
		在web项目中,"."代表在tomcat/bin目录下开始,所以不能使用这种相对路径。
		
	1.给服务器使用。		/	表示在当前web应用的根目录下(WebRoot下面)
	2.给浏览器使用。		/	表示在webapps的根目录下
*/
public class ResourceDemo extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 在java项目中这样获取,但是web项目中,这样获取不行。
		 * 
		 */
		/*File file = new File("./src/db.properties");
		FileInputStream in = new FileInputStream(file);*/
		
			
	/*	File file = new File(".");
		System.out.println(file.getAbsolutePath());//web项目中“.”代表的位置是tomcat服务器的bin目录下
*/		
	/*	File file = new File("../webapps/test/WEB-INF/classes/db.properties");
		System.out.println(file.getAbsolutePath());//这里代表的位置是tomcat服务器的bin目录的上一级目录下的\webapps\test\WEB-INF\classes\db.properties
		FileInputStream in = new FileInputStream(file);*/
		
		/**
		 * 使用web加载资源文件的推荐方式
		 */
		/*
		 * 方式1:this.getServletContext().getRealPath  读取,返回资源文件 的绝对路径。
		 */
	/*	String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
		System.out.println(path);//G:\IT_ConfigurationFile\apache\apache-tomcat-7.0.73\webapps\test\WEB-INF\classes\db.properties
		File file = new File(path);
		FileInputStream in = new FileInputStream(file);*/
		
		/*
		 * 方式2:this.getServletContext().getResourceAsStream 读取,返回资源文件的输入流
		 */
		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
		
		
		Properties prop = new Properties();
		//读取资源文件
		prop.load(in);
		
		String user = prop.getProperty("user");
		String password = prop.getProperty("password");
		
		System.out.println("user=" + user);
		System.out.println("password=" + password);
		
	}
}
资源文件:

 
在web项目中普通java文件中用“.”方式获取资源
 例如:    
File file = new File("../webapps/test/WEB-INF/classes/db.properties");
		System.out.println(file.getAbsolutePath());//这里代表的位置是tomcat服务器的bin目录的上一级目录下的\webapps\test\WEB-INF\classes\db.properties
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值