URL is directory or file

M

Mike Mimic

Hi!

How can I find out if an URL is a directory or a file?

I think that this is possible only by connecting to the
server. And after all redirects check URL for last character.
If it is "/" than it is directory. But the problem I have is
that I do not know how can I get this redirected URL.

Is there some better way to found out if something like
http://www.somepage.com/path is file or directory?


Mike
 
F

Flip

How can I find out if an URL is a directory or a file?
If you have a URL object, you can create a URI object off of that, use that
in the File constructor and then you can run the isFile() or isDirectory().

It's a bit convoluted with the URI, URL, and File, but it'll give you what
you're looking for.
 
J

Johan Poppe

Mike Mimic skrev:
How can I find out if an URL is a directory or a file?

I think that this is possible only by connecting to the
server. And after all redirects check URL for last character.
If it is "/" than it is directory. But the problem I have is
that I do not know how can I get this redirected URL.

Is there some better way to found out if something like
http://www.somepage.com/path is file or directory?

The question is rather meaningless. A URL points to a resource. That
resource can be several different things - for example output from a
script, a static file on the server, or a dynamically generated
listing of directory contents (which in a way is also a script, just
built into the web server.) There is generally no way to know if the
result you get back is static or dynamic.

Within a typical webserver setup, these URLs may return the same
static file:
http://www.somepage.com/path
http://www.somepage.com/path/
http://www.somepage.com/path/index.html

The first two might however also return a dynamic directory listing.
Or an error message telling you that the directory doesn't allow
listing.
 
M

Mike Mimic

Hi!
If you have a URL object, you can create a URI object off of that, use that
in the File constructor and then you can run the isFile() or isDirectory().

It's a bit convoluted with the URI, URL, and File, but it'll give you what
you're looking for.

It does not work. Because File wants URI with a scheme equal to "file"
and not "http":

uri - An absolute, hierarchical URI with a scheme equal to "file", a
non-empty path component, and undefined authority, query, and fragment
components


Mike
 
R

Roedy Green

How can I find out if an URL is a directory or a file?

There is nothing in the HTTP protocol that forces a server to divulge
that information. The URLs you send it may represent, dirs, files or
programs, or something completely virtual. It is none of your
business.

I suppose you could try downloading it. Even if it is a dir, you will
get a dir listing or index.html

Keep in mind you can't even get a dir listing unless the server feels
like giving you one. Servers in general hold their cards tightly to
their chests. The less hackers know, the better.
 
M

Mike Mimic

Hi!

Roedy said:
There is nothing in the HTTP protocol that forces a server to divulge
that information. The URLs you send it may represent, dirs, files or
programs, or something completely virtual. It is none of your
business.

I suppose you could try downloading it. Even if it is a dir, you will
get a dir listing or index.html

Keep in mind you can't even get a dir listing unless the server feels
like giving you one. Servers in general hold their cards tightly to
their chests. The less hackers know, the better.

Yes. But if server redirects user from some/path to some/path/ before
it sends content this probably means it is a directory. I would like
to detect this.


Mike
 
M

Michael Borgwardt

Mike said:
Yes. But if server redirects user from some/path to some/path/ before
it sends content this probably means it is a directory.

*probably*. It's neither sufficient nor necessary.
I would like
to detect this.

A HTTP redirect is done via a status code beginning with 3:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

You shouldn't even get any content unless your HTTP client library
resolves this transparently. If it does, either tell it not to or
use a different library.
 
C

Chris Smith

Mike said:
But I would like to know when http://www.somepage.com/path is the
same as http://www.somepage.com/path/ (that it has been redirected).

Right. Now that you've clarified your problem, the solution is simple.
Connect the the server, and look for a 302 response directing you to add
a '/' at the end. Are you having trouble with this?

I'd use Jakarta Commons-HttpClient myself, because I think the Java URL
and URLConnection package is overly generic to the point of useless for
a good bit of non-trivial HTTP stuff, and it's easier to switch to an
alternative than determine whether URLConnection can solve your problem
or not.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

chris

Mike said:
Hi!



Yes. But if server redirects user from some/path to some/path/ before
it sends content this probably means it is a directory. I would like
to detect this.

It's not clear from the Javadoc and I haven't tried it, but: maybe in that
case if you tell the URLConnection to connect() and then call getURL() or
toString() you will see that the trailing slash has been added.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top