结论

getClass().getResource("/") 
== getClass().getClassLoader().getResource("")
== getClass().getClassLoader().getResource("/")
== servletContext.getResourceAsStream("/WEB-INF/classes/")
	[↑ javax.Servlet Web项目]
== 硬盘上 classes 文档的全路径
如:file:/H:/MyProgramFriles/tomcat/apache-tomcat-8.5.50/webapps/aa/WEB-INF/classes/

同:spring中的classpath

另外:

public class LoginFilter extends HttpFilter implements Filter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
    throws IOException, ServletException {
    	String URL = req.getRequestURL().toString();
    	String uri = req.getRequestURI();
    	String contextPath = req.getContextPath();
    	System.out.println(URL);
    	System.out.println(uri);
    	System.out.println(contextPath);
   	    }
}

↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

测试

路径问题一切要看编译后的文件路径

比如,源文件路径是:

而编译后的文件路径为:

也就是说,resources文件夹下的文件在编译后,都是为根目录,这种情况下,比如我要读取resources 文件夹下的 my.txt 文件路径,

写法①:

// 编译后,项目根路径,写法1
String url1 = A.class.getClassLoader().getResource("").toString();
// 编译后,项目根路径,写法2
String url2 = A.class.getResource("/").toString();
// 编译后,文件根路径
String url3 = A.class.getResource("").toString();

输出:

file:/D:/IdeaWorkspace/test-null-project/target/classes/
file:/D:/IdeaWorkspace/test-null-project/target/classes/
file:/D:/IdeaWorkspace/test-null-project/target/classes/com/convict/

写法②:

// 还有一种getResource 后调用 getFile方法,就没有 [file:],而是以 [/D:xxx] 这样的格式
String url4 = A.class.getClassLoader().getResource("").getFile();
String url5 = A.class.getResource("/").getFile();
String url6 = A.class.getResource("").getFile();
System.out.println(url4);
System.out.println(url5);
System.out.println(url6);

输出:

/D:/IdeaWorkspace/test-null-project/target/classes/
/D:/IdeaWorkspace/test-null-project/target/classes/
/D:/IdeaWorkspace/test-null-project/target/classes/com/convict/

写法③:

String url7 = A.class.getClassLoader().getResource("").getPath();
String url8 = A.class.getResource("/").getPath();
String url9 = A.class.getResource("").getPath();
System.out.println(url7);
System.out.println(url8);
System.out.println(url9);

输出:

/D:/IdeaWorkspace/test-null-project/target/classes/
/D:/IdeaWorkspace/test-null-project/target/classes/
/D:/IdeaWorkspace/test-null-project/target/classes/com/convict/

至此,上面已经有多种获取文件路径的方式,既然拿到路径,就可以读取File 文件,进而读取文件了,如这是my.txt 的内容

则读取文件内容的代码可以如下:

// 获取路径
String filePath = A.class.getClassLoader().getResource("my.txt").getFile();
File my = new File(filePath);
InputStreamReader isr = new InputStreamReader(new FileInputStream(my), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
String lineTxt;
while ((lineTxt = br.readLine()) != null) {
    System.out.println(lineTxt);
}
br.close();

输出:

扩展:

写法②getFile 跟写法③getPath 是有区别的,getResource 方法后返回的是一个 java.net.URL对象,那手动构造一个java.net.URL

URL url = new URL("https://www.baidu.com/img/baidu_jgylogo3.gif?param=1111&param2=aaaaa");
System.out.println("getFile ==> " + url.getFile());
System.out.println("getPath ==> " + url.getPath());

输出:

getFile ==> /img/baidu_jgylogo3.gif?param=1111&param2=aaaaa
getPath ==> /img/baidu_jgylogo3.gif

可以看出,getFile() 方法可以拿到参数值,而getPath() 就是只取文件名,JDK中文文档相关api如下: