- context
获取真实路径(*****)
还可以使用ServletContext对象来获取Web应用下的资源,例如在hello应用的根目录下创建a.txt文件,现在想在Servlet中获取这个资源,就可以使用ServletContext来获取。
l 获取a.txt的真实路径:String realPath = servletContext.getRealPath(“/a.txt”),realPath的值为a.txt文件的绝对路径:F: omcat6webappshelloa.txt;
l 获取b.txt的真实路径:String realPath = servletContext.getRealPath(“/WEB-INF/b.txt”);
5.2 获取资源流
不只可以获取资源的路径,还可以通过ServletContext获取资源流,即把资源以输入流的方式获取:
l 获取a.txt资源流:InputStream in = servletContext.getResourceAsStream(“/a.txt”);
l 获取b.txt资源流:InputStream in = servletContext.getResourceAsStream(“/WEB-INF/b.txt”);
5.3 获取指定目录下所有资源路径
还可以使用ServletContext获取指定目录下所有资源路径,例如获取/WEB-INF下所有资源的路径:
Set set = context.getResourcePaths("/WEB-INF"); System.out.println(set); |
[/WEB-INF/lib/, /WEB-INF/classes/, /WEB-INF/b.txt, /WEB-INF/web.xml] |
注意,本方法必须以“/”开头!!!
- class和classloader
获取类路径下资源
获取类路径资源,类路径对一个JavaWeb项目而言,就是/WEB-INF/classes和/WEB-INF/lib/每个jar包!
l Class
l ClassLoader:
这里要讲的是获取类路径下的资源,对于JavaWeb应用而言,就是获取classes目录下的资源。
InputStream in = this.getClass().getResourceAsStream("/xxx.txt"); System.out.println(IOUtils.toString(in)); |
InputStream in = this.getClass().getClassLoader().getResourceAsStream("xxx.txt"); System.out.println(IOUtils.toString(in)); |
l Class类的getResourceAsStream(String path):
- 路径以“/”开头,相对classes路径;
- 路径不以“/”开头,相对当前.class文件所有路径,例如在cn.itcast.servlet.MyServlet中执行,那么相对/classes/cn/itcast/servlet/路径;
l ClassLoader类的getResourceAsStream(String path):
- 相对classes路径;
实例
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); Integer count = (Integer) servletContext.getAttribute("count"); if (count == null) { count = 1; servletContext.setAttribute("count",1); } else { servletContext.setAttribute("count",count + 1); } PrintWriter pWriter = response.getWriter(); pWriter.write("<h1>"+count+"</h1>"); System.out.println(count); String string = servletContext.getRealPath("/index.jsp"); System.out.println(string); InputStream inputStream = servletContext.getResourceAsStream("/index.jsp"); Set<String> strings = servletContext.getResourcePaths("/WEB-INF"); System.out.println(strings); ClassLoader classLoader = servletContext.getClassLoader(); InputStream inputStream2 = classLoader.getResourceAsStream("/a.txt"); InputStream inputStream3 = classLoader.getResourceAsStream("/com/lqkj/context/b.txt"); String string2 = IOUtils.toString(inputStream2); System.out.println(string2); String string3 = IOUtils.toString(inputStream3); System.out.println(string3); Class class1 = this.getClass(); InputStream inputStream4 = class1.getResourceAsStream("b.txt"); System.out.println(IOUtils.toString(inputStream4)); InputStream inputStream5 = class1.getResourceAsStream("/a.txt"); System.out.println(IOUtils.toString(inputStream5)); InputStream inputStream6 = class1.getResourceAsStream("/../../index.jsp"); System.out.println(IOUtils.toString(inputStream6)); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1. 得到ClassLoader * > 先得到Class,再得到ClassLoader * 2. 调用其getResourceAsStream(),得到一个InputStream */ // ClassLoader cl = this.getClass().getClassLoader(); // 相对/classes // InputStream input = cl.getResourceAsStream("cn/itcast/servlet/a.txt"); Class c = this.getClass(); // 相对当前.class文件所在目录! // InputStream input = c.getResourceAsStream("a.txt"); // 相对classes下! // InputStream input = c.getResourceAsStream("/a.txt"); InputStream input = c.getResourceAsStream("/../../index.jsp"); String s = IOUtils.toString(input);//读取输入流内容,转换成字符串返回 System.out.println(s); }