First, Java is getResourceAsStream gets the specified resource in the project are the following:
A: Gets the resources under src
. 1), Class.getResourceAsStream (String path): no path to '/' default from the package where such resources taken at the beginning, with '/' is from the beginning of the root ClassPath (i.e., '/' represents src) Get . It is only by constructing a path absolute path, and ultimately access to resources by the ClassLoader.
A)、在同级目录下:com.x.y 下有类me.class ,同时在同级目录下有资源文件myfile.xml,则应使用:me.class.getResourceAsStream("myfile.xml");
B)、在子目录下:例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml,则应该使用me.class.getResourceAsStream("file/myfile.xml");
2), Class.getClassLoader.getResourceAsStream (String path): The default is taken from the root ClassPath, path not begin with '/', is ultimately acquired by the resource ClassLoader.
A)、 不在同级目录下,也不在子目录下:例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml,则应该使用:me.class.getClassLoader.getResourceAsStream("com/x/file/myfile.xml");
Two: Gets the specified resource in the web project
1) , the ServletContext getResourceAsStream. (String path): default (that is, from WebAPP root: root directory of the project to be published in the server (with the same level of web files under src folder)) to take the resource under, path whether '/ 'beginning does not matter;
A)、在web项目的根目录下有myfile.xml文件,则应该使用:
getServleContext().getResourceAsStream("myfile.xml");
2), file application in the built-in objects is a kind Jsp to achieve the above ServletContext
forward from: