This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Services and the fly likes How to get all files in a subdirectory of a webpage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to get all files in a subdirectory of a webpage" Watch "How to get all files in a subdirectory of a webpage" New topic
Author

How to get all files in a subdirectory of a webpage

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Hey all,

In my Java program, I need to know how to determine how many PDF files there are in the directory and their names. I know how to do this on the local system:


this will print out the names of all files in the directory that are of type PDF.

does anyone know how to do this in a Web directory?

Thanks,
Andrew
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
You can't. Unless there's a web page you can parse that specifically links to each and every one of those files, there's no reliable way to be sure you got all of them.


Ping & DNS - my free Android networking tools app
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Thanks Ulf, I'll try to get around it a different way.
Now I have written a class that sits in the same folder as the PDF files and returns all the names in one of its methods.


Is there anyway to call this through a URL such as "http://<webname>/<directory>/"?
because I know i can access the PDF files through a URL.

thanks for your help,
Andrew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, of course you can't call that class directly through an HTTP URL. But you could certainly have the URL request a servlet which called that class and did something useful like turning the list of files into HTML.

And you'll have to fix it so that the class is told what directory to look in. Having it use the current working directory is going to fail when you run it in an application server.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Thanks for helping me out again Paul.
So I've looked into servlets and tried to write one that will create a string of file names that my applet can get from it.

Here's the Servlet:


and here's a test applet that uses it:


i get this error:
unknown protocol c:


when I put this applet on the website i will be accesing the servlet through HTTP. will that fix it? I can't put it up until it works..

Thanks for any help,
Andrew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well of course you can't access your server's C: drive directly from the applet, which will be running on a different computer.

You're going to have to stop thinking about accessing a folder over the network and start thinking about an application which allows the client to do something like that. Basically you're going to need two requests: the first returns a list of the available names (note: the name doesn't include the path, just the name of the file) and the second returns a specific file given a name.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
So to access a servlet would you just put the servlet's class file (Servlet.class) in the correct server folder and then in the applet use the code:

?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

In an applet? No, I wouldn't hard-code the host name like that. Use the getCodeBase() method to get the URL where the applet was downloaded from, and then create a URL which is relative to that.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
I would do that, except the applet and servlet are on different servers. So I need the "http://...." don't I?
also would I need ".class" after the servlet class name?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get all files in a subdirectory of a webpage
 
Similar Threads
About for loop of jdk1.5
File sepearator for unix/windows problem
Java directory
Merging byte arrays using SequenceInputStream
exe file to get the property of the files in a folder