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);
相关文章推荐
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- ServletContext读取Web应用中的资源文件
- ServletContext读取web应用中的资源文件
- java_web11-ServletContext读取Web应用中的资源文件
- 利用ServletContext对象读取web应用中的资源文件
- Servlet之ServletContext读取web应用中的资源文件
- ServletContext 读取Web应用中的资源文 4000 件
- javaweb读取 web 应用中的资源文件
- web应用中路径问题以及读取web应用下资源文件
- 使用Spring开发web应用,读取外部资源文件问题
- WEB应用中读取资源文件的方式有三种
- WEB应用中的普通Java程序如何读取资源文件
- 读取web应用下的资源文件(例如properties)
- servletContext对象之读取web项目下文件资源
- 第11天(就业班) servlet编程、读取web应用下的资源文件、cookie、Session技术
- servletcontext读取web资源文件
- java_web12-WEB应用中的普通Java程序如何读取资源文件
- web项目中servlet利用servletContext对象读取资源文件
- web应用中普通java程序读取资源文件
- ServletContext_资源文件3(文件在web应用下)