Steven Landow
Steven Landow

Reputation: 153

List files/subfolders on apache server via HTTP in Java

So I can access a file on HTTP using

URL url = new URL("www.example.com/file.extension");

Then I would get a InputStream stream = url.openStream(); and do stuff with that. Is there a way that if I specifiy http://www.example.com/ I can loop through and get a list of all urls to each file?

Upvotes: 1

Views: 3870

Answers (2)

Steven Landow
Steven Landow

Reputation: 153

The server in question was an Apache server. As Nathan said in his comment, directory indexing had to be enabled.

Enable Indexing

See: http://www.cyberciti.biz/faq/enabling-apache-file-directory-indexing/

Approach 1:

  1. Open Apache config file: (etc/httpd/httpd.conf or etc/apache2/apache2.conf)
  2. Add the following:

    <Directory /var/www/example.com/directory>
    Options Indexes FollowSymLinks
    </Directory>
    
  3. Save and close

  4. Restart Apache

Approach 2:

  1. Create/Open a file called .htaccess in directory you want indexed.
  2. Append the following line: Options Indexes

Use this in Java

Now that indexing is enabled you can actually access the list of subdirectories/files from your java code.

URL url = new URl("http://www.example.com/directory"); //or just "http://www.example.com/" for the root dir, if there isn't an index.html

Scanner sc = new Scann(url.openStream());
while(sc.hasNextLine())
      System.out.println(sc.nextLine());

The output of this snippet will be

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of directory</title>
  </head>
  <body>
<h1>Index of directory/</h1>
<ul><li><a href="subdir/">subdir</a></li>
<li><a href="file.txt">file.txt</a></li>
</ul>
</body></html>

Parse that URL however you choose. You will also get a directory listing as a webpage if you visit http://www.example.com/directory/

Upvotes: 2

tragle
tragle

Reputation: 121

Not through the http server. If you're running this on the same machine, you can use the normal Java File API.

Upvotes: 0

Related Questions