简体   繁体   中英

how to find out if the url http://www.test.com/abc.pdf is a file or a directory?

i want to write some code in java to find out if a given url is a file or a directory. how can i do this??

URLs themselves don't have the concept of being a "file" or a "directory". The content of a URL is defined by whatever the server responds with when requested. If you get something with a MIME type of application/pdf , then the URL represents a PDF file. If you get anything else, then it's not a PDF file.

There is simply no notion of a "directory" in any of the URL / URI specs, the HTTP specs or the MIME type registry.

So the webserver has no way of telling the client that a URL resolves to a directory ... even if it knows what that means. (And in many cases, the webserver doesn't know / care about directories itself; eg a typical RESTful web API doesn't recognize the concept.)

Your options are:

  • Try to fetch things and see what content type you get. But bear in mind that a "directory" might be rendered by the webserver as anything ... so it is (in general) impossible to reliably distinguish directories from non-directories this way.

    If you wanted to avoid downloading the file, you could send a HEAD request instead of a GET request. This requires the use of a fully fledged HTTP client library rather than URLConnection .

  • Change your application design and implementation so that the "directory" concept is not required.

  • Change your application so that it decides what is a "directory" and what is a "file" based purely on the URLs. (In the general case, this won't work ... because there are no universally observed conventions for URL name parts that would allow you to make the distinction.)

  • Change to using a URL scheme / protocol in which "directory" is a well-defined concept. For instance "file:" or "ftp:".

What you get back from a URL essentially isn't a "file" or a "directory." At best, it's a stream of data with a content type. It generally becomes a "file" on the client side, either by means of saving it to a file system or to a temporary store for display only. Basically, there's no way for a web server to tell a client that something is a directory using HTTP.

You're either going to have to create some client-side business logic to infer a "directory" (possibly based on the URL, maybe a lack of file extension?) or use a different protocol for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM