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?
First of all,
Filewon’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,
URLConnectionand parse it yourself.To list files over FTP, you could use
FTPClientfrom Apache Commons Net.