Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Url refers to file or DIrectory. (HTTP)

How can I determine if an url is referring to a file or directory. the link http://example.com/test.txt should return that it is a file an http://example.com/dir/ is a directory

I know you can do this with the Uri class but this objects IsFile function only works with the file:/// scheme. And I am working with the http:// scheme. Any idea's?

Thanks

like image 380
user1939247 Avatar asked Dec 31 '12 10:12

user1939247


1 Answers

I'm not sure why this question was left unanswered/neglected for a long time. I faced the same situation in server-side java (reckon it would be similar for Android flavour). The only input information is a URL to the resource and we need to tell if the resource is a directory or file. So here's my solution.

if ("file".equals(resourceUrl.getProtocol())
    && new File(resourceUrl.toURI()).isDirectory()) {
    // it's a directory
}

Hope this helps the next reader.

Note: please see @awwsmm comment. It was my assumption when provided the answer above. Basically, it doesn't make sense to test if a remote resource is a directory or anything. It is totally up to the site to decide what to return for each request.

like image 155
t7tran Avatar answered Oct 11 '22 19:10

t7tran