servlet–獲取類路徑下資源
- 2019 年 10 月 10 日
- 筆記
- context
獲取真實路徑(*****)
還可以使用ServletContext對象來獲取Web應用下的資源,例如在hello應用的根目錄下創建a.txt文件,現在想在Servlet中獲取這個資源,就可以使用ServletContext來獲取。
l 獲取a.txt的真實路徑:String realPath = servletContext.getRealPath(「/a.txt」),realPath的值為a.txt文件的絕對路徑:F:tomcat6webappshelloa.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); }