使用getClassLoader().getResource()需注意的问题

获取src资源文件编译后的路径(即classes路径):

String urltest = jdbcTest.class.getClassLoader().getResource("").getPath();
System.out.println("urltest = " + urltest);

获取用户的当前工作目录:

urltest = System.getProperty("user.dir");
System.out.println("urltest = " + urltest);

输出结果:

urltest = /E:/code/jdbcTest/out/production/jdbcTest/
urltest = E:\code\jdbcTest

第二这个是我们在编写的java源代码文件在电脑上的保存位置的根目录。


在Java WEB项目编程时,往往通过classLoader获取Jar包的物理路径,但是可能会存在2个问题:URL地址解析错误,不能正确获取Jar文件路径。

  1、URL地址解析错误

  见下例,通过getClassLoader().getResource()获取到路径后,会发现使用File类去打开,却打不开,原因是url路径使用了url编码,空格被转义为"%20"字符,这时,使用File类通过该路径是不能找到想要找的文件的。


  URL url = AClass.class.getClassLoader().getResource("/");

  String libpath = url.getPath();

  File file = new File(libpath);

  if(file.exists()){

  //do something

  }

  正确的做法是对文件路径进行转义,使用 URLDecoder.decode(PATH, "UTF-8"):


  URL url = AClass.class.getClassLoader().getResource("/");

  String libpath = url.getPath();

  String libpath2 = URLDecoder.decode(libpath, "UTF-8");

  File file = new File(libpath2);

  2、不能正确获取Jar文件路径

  在WEB项目中,发现通过getClassLoader().getResource()不能正确地获取到jar文件的路径,获取到的路径是应用服务器(如Tomcat的类路径),这是因为jar包不是被APPClassLoader所装载,而是被Tomcat Ext ClassLoader所装载,获取的的路径没有指向WEB服务器的WEB-INF/lib路径。

  解决的办法是,在WEB-INF目录下放置一个名为“classes”的文件夹,这样App ClassLoader才会被用来装载WEB-INF/lib下的jar包,才能正确获取到jar文件的路径。
————————————————————
  在开发中经常需要获取资源文件路径,例如读写配置文件等。Java也提供很多方法来获取这些路径,下面就几种常用到的作一下讨论区分:

  1、xxx.class.getClassLoader().getResource(“”).getPath();

  获取src资源文件编译后的路径(即classes路径)

  2、xxx.class.getClassLoader().getResource(“文件”).getPath();

  获取classes路径下“文件”的路径

  3、xxx.class.getResource(“”).getPath();

  缺少类加载器,获取xxx类经编译后的xxx.class路径

  4、this.getClass().getClassLoader().getResource(“”).getPath();

  以上三种方法的另外一种写法

  5、request().getSession().getServletContext().getRealPath(“”);

  获取web项目的路径

  例如:

  System.out.println(FileUpLoadAction.class.getClassLoader().getResource("conf.properties").getPath());

  System.out.println(FileUpLoadAction.class.getClassLoader().getResource("").getPath());

  System.out.println(FileUpLoadAction.class.getResource("").getPath());

  System.out.println(this.getClass().getClassLoader().getResource("").getPath());

  System.out.println(ServletActionContext.getRequest().getSession().getServletContext().getRealPath("login.jsp"));

  输出依次为:

  /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/conf.properties

  /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/

  /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/com/gmi/client/

  /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/

  D:\codes\trunk\client\web\IBMS\myworkspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\webclient\login.jsp

  此外,对于读写配置文件,例如properties文件,Java还提供了getResourceAsStream(“文件”)方法返回一个InputStream供读取文件:

  InputStream is = this.getClass().getClassLoader().getResourceAsStream(“conf.properties”);

  好了,以上也只是一部分方法,还有诸多不足之处,后续补充,欢迎指正!!!

posted @ 2021-09-01 14:59  meteor119  阅读(1324)  评论(0)    收藏  举报