Java中获取资源文件的方法总结

这里总结3中方法获取资源文件的

  • ServletContext
  • Class
  • ClassLoader

文件的位置

 

1. ServletContext#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        ServletContext context = this.getServletContext();
        /**
         * 获取不同路径下的资源文件
         * servletContext是相对于项目的根目录的,这里为WebContent
         */
        InputStream  inputA = context.getResourceAsStream("/a.txt");
        InputStream inputB = context.getResourceAsStream("/WEB-INF/classes/cn/zydev/b.txt");
        InputStream inputC = context.getResourceAsStream("/WEB-INF/classes/c.txt");
         
        /**
         * 获取真实的磁盘路径
         */
        String realPath = context.getRealPath("/WEB-INF/classes/c.txt");
         
        /**
         * 获取指定目录下的文件(包括目录,深度为1级)
         */
        Set<String> rsc = context.getResourcePaths("/WEB-INF");
        String a = IOUtils.toString(inputA);
        String b = IOUtils.toString(inputB);
        String c = IOUtils.toString(inputC);
        pw.print(a+"<br/>");
        pw.print(b+"<br/>");
        pw.print(c+"<br/>");
        pw.print(realPath+"<br/>");
        pw.println(rsc);
    }

  结果显示:

 2. ClassLoader#

使用ClassLoader是相对于classes的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        /**
         * ClassLoader是相对于classes参照的
         * 第一个斜杠可以不写,也可以写成./(熟悉Linux的应该很清楚)
         */
        ClassLoader cl = this.getClass().getClassLoader();
        InputStream inputA = cl.getResourceAsStream("/../../a.txt");
        InputStream inputB = cl.getResourceAsStream("/cn/zydev/b.txt");
        InputStream inputC = cl.getResourceAsStream("/c.txt");
        String a = IOUtils.toString(inputA);
        String b = IOUtils.toString(inputB);
        String c = IOUtils.toString(inputC);
        pw.print(a+"<br/>");
        pw.print(b+"<br/>");
        pw.print(c+"<br/>");
    }

  得到结果:

3. class#

路径前斜杠表示相对于当前的class,不加斜杠相对于classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        Class cs = this.getClass();
        //不加斜杠表示相对于class(CServlet)
        InputStream inputA = cs.getResourceAsStream("../../../../a.txt");
        InputStream inputB = cs.getResourceAsStream("b.txt");
        //加斜杠,相对于classes
        InputStream inputC = cs.getResourceAsStream("/c.txt");
        String a = IOUtils.toString(inputA);
        String b = IOUtils.toString(inputB);
        String c = IOUtils.toString(inputC);
        pw.print(a+"<br/>");
        pw.print(b+"<br/>");
        pw.print(c+"<br/>");
    }

  得到结果:

 

posted @   头痛不头痛  阅读(954)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩