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 of a YouTube video, on save the page will auto display the embedded YouTube player.
When a user posts URL of a picture in Flickr, on save, the page will auto display a smaller version of the Flickr image.
You can fetch the URL and see Content-type from the response.
You can use the HTTP Client from apache, it helps you to fetch the content of the URL and you can use it to navigate the redirects. For instance try to fetch the following:
http://www.youtube.com/watch?v=d4LkTstvUL4
Will return an HTML containing the video. After a while you'll find out the video is here:
http://www.youtube.com/v/d4LkTstvUL4
But if you fetch that page you will get a redirect:
So, what you have to do is to fetch the URL and examine it, until you get final content
This section explains how to handle the redirects.
This is a solution without apache.
Follow Redirect Example from mkyong.com
Hit the link and inspect the content type header? If the result is a HTML page you could look for the largest image or embedded flash file on the page and choose to display that?
I suggest using curl with a range header to allow you to inspect the file type itself.
Then execute:
Now you know the mime type: image/png, the file size 3438 bytes, and the file is a 250 x 61 color PNG image.
Fast video indexer is a video capture software that can capture video frames automatically from a list of videos and create index web pages, index pictures or a list of images.
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":
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.