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

2011-07-18 Thread Christopher Schultz
-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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



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

2011-07-17 Thread Pid *
On 16 Jul 2011, at 15:46, Dark Before Dawn dark.before.d...@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/contentbr/);
while ((line = br.readLine()) != null) {
// print file content
out.write(line +br/);
}
br.close();


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


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