简体   繁体   中英

Problem loading resources from classpath


In a web application running in a local tomcat, I am trying to load a folder /folder which is in tomcat/webapps/myproject/WEB-INF/folder To do it:

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder");  

Which returns null . This piece of code is supposed to load resources from classpath, which is if I am not wrong in the path where my folder is in.
Anyway, I moved my folder to different paths, such as tomcat/webapps/myproject/WEB-INF/classes/folder or tomcat/webapps/myproject/WEB-INF/lib/folder with the same result.
Did I miss something? Thanks in advance.

Regarding on all your answers (thanks), I edit my question with all I have tryed, with the same null result.
A)

 String realSource = getServletContext().getRealPath("/folder");  

B)

InputStream realPath = getClass().getClassLoader().getResourceAsStream("/folder/fileInFolder"); 

C)

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
String realSource = servletContext.getRealPath("/folder");

I must say that my folder path is tomcat/webapps/myproject/WEB-INF/classes/folder

The problem is that /WEB-INF is not in the CLASSPATH of a WAR file.

/WEB-INF/classes is in the CLASSPATH; so are all the JARs in /WEB-INF/lib.

If you put the file you need into /WEB-INF/classes the WAR class loader should be able to find it.

You cannot read a directory that way. It must be a file.

UPDATE:

Anyway, I moved my folder to different paths, such as tomcat/webapps/myproject/WEB-INF/classes/folder or tomcat/webapps/myproject/WEB-INF/lib/folder with the same result. Did I miss something?

Yes, you've missed two things:

  1. Your folder should go in one place - under /WEB-INF/classes
  2. You cannot access the folder using getResourceAsStream() and read all the contents. It doesn't work that way. You can only read one individual file that way.

If you want the full path of the folder within your webapp so that you can use File operations on it, here is a way to do it.

Note: myfolder is parallel to WEB-INF within my webapp

String relativeWebPath = "/myfolder";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

Now printing out absoluteDiskPath gives me

D:\apache-tomcat-6.0.20\webapps\mywebapp\myfolder

Try listing files in that folder by

File dir = new File(absoluteDiskPath); 

String[] children = dir.list(); 
if (children != null) 
{ 

    for (int i=0; i<children.length; i++) { 
    // Get filename of file or directory 

    String filename = children[i]; 
    System.out.println("filename no " + i + filename);
    } 
}

删除前导斜杠并尝试ie

InputStream realPath = getClass().getClassLoader().getResourceAsStream("folder");  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM