Advice needed on OS Independent directory listing via Servlet

L

Lord0

Hi there,

I've got a servlet (running on Tomcat) which contains a method for listing
the contents of a given directory on the machine on which it is running:

// snip

File allFiles = new File(myPath); // Absolute path
String files[]=allFiles.list();

// snip

So we get an array containing Strings corresponding to all the files in a
named directory.

This works fine and does what I need it to do. However when I move the
servlet from my Windoze machine (dev environment) to my linux machine (live
environment) I run into difficulties because the file paths are obviously
different. I would like to do it in such a way that the servlet *does not
need to be edited*. I had thought of a couple of possible solutions but at
the moment they are pretty vague and therefore I am looking for suggestions
from you kindselves.

My early ideas:

1. Indicate, somehow, in the server.xml, a "root" path which I can access
from within the servlet, which on the Windoze and Linux box would have a
different value.
2. Use JNDI?

So anyway, your thoughts would be appreciated

Lord0
 
P

Paul Lutus

Lord0 said:
Hi there,

I've got a servlet (running on Tomcat) which contains a method for listing
the contents of a given directory on the machine on which it is running:

// snip

File allFiles = new File(myPath); // Absolute path
String files[]=allFiles.list();

// snip

So we get an array containing Strings corresponding to all the files in a
named directory.

This works fine and does what I need it to do. However when I move the
servlet from my Windoze machine (dev environment) to my linux machine
(live environment) I run into difficulties because the file paths are
obviously different.

What difficulties? You are obviously encountering a portability issue, but
it would be better if you didn't make us guess.
I would like to do it in such a way that the servlet
*does not need to be edited*. I had thought of a couple of possible
solutions

Solutions to what problem? You can always use File.listRoots() to get a
portable filesystem root list.

Please be specific when you post a request for help. If you have a Windows
machine with one drive, the solution cam be very simple: "/". If there is
more than one Windows drive and if you need to choose the drive, this
scheme is not portable across platforms, period, because not all operating
systems have multiple roots.

I think a solution based on File.listRoots() will be able to cope with the
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top