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?
-
is the location an FTP server?user591593– user5915932011年10月14日 07:42:56 +00:00Commented 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.user591593– user5915932011年10月14日 07:44:08 +00:00Commented Oct 14, 2011 at 7:44
1 Answer 1
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.