You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dark Before Dawn <da...@gmail.com> on 2011/07/16 16:46:18 UTC

[JSP] List directory content within a war-file

Hi there,

I am trying to list files and directories within an war-file i.e 
images/summer2010 images/winter2010. The war-file is deployed on a 
tomcat 7 instance with unpackWARs="false".
Since I have no exposed filesystem getRealPath will return null. So I 
tried to use getResourceAsStream wich works fine for files but not for 
directories.
My other idea was to use getResource and URL/URI to get a java.io.File 
via JNDI. But this file is not a file nor a directory.
Cheers
Darky

Code Snippet:
     // null if unpackWar = false
         String absolute = 
this.getServletConfig().getServletContext().getRealPath("files");
     out.write("Files: " + absolute);

     // jndi
         URL url = 
this.getServletConfig().getServletContext().getResource("files");
     out.write("<br/>" + url.toURI());

     File f = new File("file:" + url.toURI());

     // Both null
     out.write(" isDir: " + f.isDirectory());
     out.write(" isFile: " + f.isFile());

         InputStream is = 
this.getServletConfig().getServletContext().getResourceAsStream("files/1.txt");

     // NPE if directory
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String line;
     out.write("<br/>content<br/>");
         while ((line = br.readLine()) != null) {
             // print file content
             out.write(line +"<br/>");
         }
         br.close();


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [JSP] List directory content within a war-file

Posted by Pid * <pi...@pidster.com>.
On 16 Jul 2011, at 15:46, Dark Before Dawn <da...@gmail.com> wrote:

> Hi there,
>
> I am trying to list files and directories within an war-file i.e images/summer2010 images/winter2010. The war-file is deployed on a tomcat 7 instance with unpackWARs="false".
> Since I have no exposed filesystem getRealPath will return null.

If you 'have no exposed file system', how do you expect to get files?


p

> So I tried to use getResourceAsStream wich works fine for files but not for directories.
> My other idea was to use getResource and URL/URI to get a java.io.File via JNDI. But this file is not a file nor a directory.
> Cheers
> Darky
>
> Code Snippet:
>    // null if unpackWar = false
>        String absolute = this.getServletConfig().getServletContext().getRealPath("files");
>    out.write("Files: " + absolute);
>
>    // jndi
>        URL url = this.getServletConfig().getServletContext().getResource("files");
>    out.write("<br/>" + url.toURI());
>
>    File f = new File("file:" + url.toURI());
>
>    // Both null
>    out.write(" isDir: " + f.isDirectory());
>    out.write(" isFile: " + f.isFile());
>
>        InputStream is = this.getServletConfig().getServletContext().getResourceAsStream("files/1.txt");
>
>    // NPE if directory
>        BufferedReader br = new BufferedReader(new InputStreamReader(is));
>        String line;
>    out.write("<br/>content<br/>");
>        while ((line = br.readLine()) != null) {
>            // print file content
>            out.write(line +"<br/>");
>        }
>        br.close();
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [JSP] List directory content within a war-file

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

DBD,

On 7/16/2011 10:46 AM, Dark Before Dawn wrote:
> Since I have no exposed filesystem getRealPath will return null. So
> I tried to use getResourceAsStream wich works fine for files but not
> for directories.

Are you sure you want to do this?

> My other idea was to use getResource and URL/URI to get a
> java.io.File via JNDI. But this file is not a file nor a directory.

How is this file not a file?

> // null if unpackWar = false String absolute = 
> this.getServletConfig().getServletContext().getRealPath("files"); 
> out.write("Files: " + absolute);
> 
> // jndi URL url = 
> this.getServletConfig().getServletContext().getResource("files");

Read the javadoc for ServletContext.getResource:

"
Returns a URL to the resource that is mapped to a specified path. The
path must begin with a "/" and is interpreted as relative to the current
context root.
"

> InputStream is = 
> this.getServletConfig().getServletContext().getResourceAsStream("files/1.txt");

This
> 
should work, as long as you're using the API properly.

> // NPE if directory

That makes sense. You need to check for null, just like the javadocs say.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4kSLsACgkQ9CaO5/Lv0PDn5wCfcXyA3/YahOeHpiiaKWTxgIcG
/9kAoJNThIueIkq+08FsWLUxT3dfZa6e
=6KeA
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org