How to determine if a given URL link is a video or image?

后端 未结 6 1155
遥遥无期
遥遥无期 2021-01-18 15:06

I\'m trying to take a given URL entered by user and determine if the URL is pointing to a image or a video.

Example use case:

When a user paste in the URL o

6条回答
  •  有刺的猬
    2021-01-18 15:10

    Issue an HTTP HEAD request so you can examine the HTTP headers that come back without having to first download the entire document. Showing a non-programmatic case under Linux using "curl":

    $ curl --head http://stackoverflow.com/Content/Img/stackoverflow-logo-250.png
    HTTP/1.1 200 OK
    Cache-Control: max-age=28800
    Content-Length: 3428
    Content-Type: image/png
    Last-Modified: Fri, 16 Jan 2009 09:35:30 GMT
    Accept-Ranges: bytes
    ETag: "98f590c5bd77c91:0"
    Server: Microsoft-IIS/7.0
    Date: Fri, 23 Jan 2009 03:55:39 GMT
    

    You can see here from the Content-Type that this is an image. You can use HTTPClient from Apache from Java to do the HTTP Head request.

    If you want to download the content for sure, then just issue the HTTP GET (using Httpclient) and use the same HTTP Header to determine the content type.

提交回复
热议问题