I’ve created a little app that browse my file system to create and display it as a tree (JTree) . Now I want to do the same for my files present on my server .
I have an Apache Tomcat server .
What I want to do is something like :
File dir = new File(new URI("file://localhost:8084/myapp/Dir"));
File[] files = dir.listFiles();
for (File f : files) {
System.out.println(f.getAbsolutePath());
this is a just an exemple but I want to use java File API .
I am getting this error when I put this code in a servlet :
11 mai 2011 10:02:15 org.apache.catalina.core.StandardWrapperValve invoke "Servlet.service()" pour la servlet tests a généré une exception
java.lang.IllegalArgumentException: URI has an authority component
at java.io.File.<init>(File.java:385)
I dont know what does this means.
I’ve also tried File dir = new File("Dir") ; and File dir = new File("web/Dir") and even File dir = new File(new URI(http://localhost:8084/myapp/Dir));
but Dir is not found . I dont get it and I am going mad (hum..sorry) .
when I type http://localhost:8084/myapp/Dir on my web browser the content of Dir is displayed .So Dir is really on http://localhost:8084/myapp/
I really really need some backup here .
Thanks in advance .
Assuming that your webapp is deployed as an unpacked war, then you can use
getRealPath()on theServletContextobject to obtain the real filesystem path for a given webapp path.So from inside your servlet:
See javadoc.
Note that this isn’t good practice, and will not work on any appserver which doesn’t unpack its WARs onto the filesystem before running it (e.g. Google App Engine).