您的位置:首页 > 编程语言 > Java开发

使用servletContext读取资源文件

2017-04-09 20:26 351 查看
//读取webroot目录下的资源
InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
System.out.println(in);

//获取web资源的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in = new FileInputStream(path);

Properties prop = new Properties();
prop.load(in);

String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");

//读取web工程中资源文件

InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);

String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web servletContex