您的位置:首页 > 其它

ServletContext读取Web应用下的文件资源

2013-07-23 15:15 337 查看
private void test() throws IOException {
InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties props=new Properties();
props.load(in);
System.out.println(props.getProperty("url"));
}

getServletContext().getResourceAsStream方法的实现是:利用相对路径获取绝对路径,再转化成输入流

private void test2() throws IOException {
String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in=new FileInputStream(path);
Properties props=new Properties();
props.load(in);
System.out.println(props.getProperty("url"));
}


getServletContext().getResourceAsStream中获取项目下的文件转化成流,下面这样写是错的

InputStream inputStream = getServletContext().getResourceAsStream("../../resources/"+multiName);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: