zoukankan      html  css  js  c++  java
  • Java中获取资源文件的方法总结

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

    • ServletContext
    • Class
    • ClassLoader

    文件的位置

    1. ServletContext

    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的

    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

    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/>");
    	}
    

      得到结果:

  • 相关阅读:
    Google's Machine Learning Crash Course #01# Introducing ML & Framing & Fundamental terminology
    MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
    MySQL笔记(二)数据库对象的创建和管理
    浅谈TCP/IP网络编程中socket的行为
    linux网络编程中的shutdown()与close()函数
    c++11中的线程、锁和条件变量
    多线程TcpServer
    TCP网络库:Acceptor、TcpServer、TcpConnection
    epoll 的accept , read, write
    线程安全函数
  • 原文地址:https://www.cnblogs.com/zydev/p/6935715.html
Copyright © 2011-2022 走看看