This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get all files in a subdirectory of a webpage

 
Andrew Cho
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Cho
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic