java: 演示获取类路径下的资源
2018-09-17 本文已影响0人
梦之志
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);
}