Some http-servers allow clients to get the directory listing of a specific directory if there's no default html page. The question is easy: how can I get that listing using Java?
It's quite easy getting a page, below you see the code needed for doing that. Just as soon as I try to use a URL which is a directory I get an iContentsize of -1, which is not much, is it?
Below is the code I use in a slightly moderated form. The parts of interest are between "start-" and "end important part". The authorization part is borrowed from Q.10009989 ("Retrieval of URL with Authorization") as is the HTUU-class. Of course, this is not needed for most sites.
Who can/want to help me with this challenge? Maybe it's quite easy, but then you're just lucky getting the points. Any answer which really gets me such a listing will get the credits.
PS: I use SilverStream, in case you were wondering about the AgoInvokedEvent. The method uses "evt.setResult()" for setting data for the calling thread.
Greetz and thanks in advance,
public void invoked(AgoInvokedEvent evt) throws Exception
Hashtable htResultData = new Hashtable();
//////////////the important part/////////////////////
URLConnection ucToGet = (new URL("http://host/path
ation", "Basic " + HTUU.encode("user:password
InputStream is = ucToGet.getInputStream();
int iContentSize = ucToGet.getContentLength()
if(iContentSize < 1) //Can't do anything in this case: a directory
throw new Exception("Contentlength is smaller then one, can't read content! (Directorylisting)");
///////////////end important part////////////////////
byte bData = new byte[iContentSize];
int b = 0, x = 0;
while ((b = is.read()) != -1 && (x < iContentSize))
htResultData.put("size", new Integer(iContentSize));
if(__E instanceof FileNotFoundException)
ion", "The URL has not been found on the server, " +
"couldn't retrieve the URL, please try again with another URL.\n\n" +
"Some other reason for this error maybe a malformed loginname/password combination.");
ion", ""); //means caller should use "error"
htResultData.put("error", __E); //Store error for retrieval for the caller