tags:

views:

59

answers:

3

Hello!
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

+3  A: 

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? Thanks in advance.

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.
duffymo
yep i didnt see this one...
smeg4brains
And how could I read the directory?? Thanks
mujer esponja
You can't, and you shouldn't.
duffymo
And there is no other way to get the path of this folder?? Apart from ServletConfig.getRealPath() ?? I wanted to do it in this way, because before gave me problems also.
mujer esponja
The better question is: what are you trying to do? I know you want to get the path of the folder and read the contents; how many files and for what purpose? What are you doing with those files?
duffymo
I want to send this folder path to another function which will process every file in this folder (the number will be different each time, maybe 10, maybe 1000). But I want to no dependn on what machine I am using, so I want to have this resources in my tomcat. Thanks
mujer esponja
What's in the folder? How do those files get there? Are you triggering this via the servlet through an HTTP request? Or s it a regularly scheduled job that you want performed every day? If it's the latter, I don't think a web app is the right way to go. A Quartz or cron job would be a better idea. I'm not sure that this belongs on a web server.
duffymo
It will be run when the user want, using this webapp interface, so cron is not the solution. In the folder will be some videos that the user will upload somehow using another service. Thanks
mujer esponja
So the user can access all the videos in the folder, even those that don't belong to them? Your use case seems muddled to me.
duffymo
Not really, admin will be able to call this function to make some process to all the videos in this folder. Normal users just will be able to upload and we will save in this same folder. SO I just need to get the path of it to can process with other functions the videos there.
mujer esponja
A: 

Remove the leading slash and try i.e.

InputStream realPath = getClass().getClassLoader().getResourceAsStream("folder");  
Suresh Kumar
I tryed also, with the same result. Thanks
mujer esponja
+1  A: 

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);
    } 
}
JoseK
Thanks, I had tryed before this way, but I had problems to init the Servlet, that's why I decided to change my way. The problem I had was in this cast Servlet servlet = (Servlet) context.getExternalContext();
mujer esponja
Can you make String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); without problems??
mujer esponja
@mujer esponja: yes, if your code is a proper servlet (i.e. extending HTTPServlet) then you can directly use getServletConfig() and getServletContext() in the goGet() method http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/servlet/http/HttpServlet.html
JoseK
I edited my question, because I am getting the same null result
mujer esponja