0

I am trying to create a method that displays a list of files in a given directory. This works fine for normal directories (on disk) but when I enter a url my list of files is null.

    public void getListOfFiles(String folderLocation){
    File folder = new File(folderLocation);
    File[] listFiles = folder.listFiles();

    for(int i = 0; i < 10; i++){
        System.out.println(listFiles[i]);
    }
}

I think my problem is because the File 'folder' is removing one of the '/' in my folderLocation (http://...)

I have tried using URL and URI but have had no luck! Can anyone help?

2
  • is the location an FTP server? Commented Oct 14, 2011 at 7:42
  • If yes, I think class File is not able to handle, you need to use some kind of FTP client library. Commented Oct 14, 2011 at 7:44

1 Answer 1

4

First of all, File won't work for this as it's not networking-aware.

Secondly, in general there's no mechanism to list files over plain HTTP. If the HTTP server gives you some kind of a listing page when you present it with the URL, you'll have to download the page using, for example, URLConnection and parse it yourself.

To list files over FTP, you could use FTPClient from Apache Commons Net.

Sign up to request clarification or add additional context in comments.

1 Comment

So I have to download the contents of the directory and then look through them myself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.