4

I asked this question before and Evgeniy Dorofeev answered it. Although worked for direct link only, but I accepted his answer. He just told me about check the content type from direct link:

String requestUrl = "https://dl-ssl.google.com/android/repository/android-14_r04.zip";
URL url = new URL(requestUrl);
URLConnection c = url.openConnection();
String contentType = c.getContentType();

As far I know, there are two URL types to download a file:

  • Direct link. For example: https://dl-ssl.google.com/android/repository/android-14_r04.zip. From this link, we can download data directly and get the file name, included with file extension (in this link, .zip extension). So we can know what file to be downloaded. You can try to download from that link.
  • Undirect link. For example: http://www.example.com/directory/download?file=52378. Have you ever tried to download data from Google Drive? When downloading data from Google Drive, it will gives you an undirect link, such as the link above. We never know whether the link contains a file or webpage. Also, we don't know the file name and file extension is, because of this link type is unclear and random.

I need to check whether it is a file or webpage. I must download it if the content type is a file.

So my question:

  • How do I check the content type from an undirect link?
  • As shown in the comments of this question, can HTTP-redirects solves the problem?

Thanks for your help.

Community
  • 1
  • 1
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87

4 Answers4

10

After you open an URLConnection, a header file is returned. There are some information about the file in it. You can pull what you want from there. For example:

URLConnection u = url.openConnection();
long length = Long.parseLong(u.getHeaderField("Content-Length"));
String type = u.getHeaderField("Content-Type");

length is size of the file in bytes, type is something like application/x-dosexec or application/x-rar.

omerfarukdogan
  • 839
  • 9
  • 26
  • From `URLConnection` class, `getHeaderField()` method returns `Content-Type` header field. This method returns the same header field with `getContentType()`. Sorry, I can't accept this answer. – Anggrayudi H Feb 16 '15 at 13:19
  • So? I don't know what the problem is, because they both work well. Which issues have you been encountering? – omerfarukdogan Feb 16 '15 at 19:02
  • It worked now. I have connected to the server twice and gives me an interval time around an hours for my IP address. When I connect to it for third times after an hours, the server returns the content type as .htm. So the problem comes when I try to re-connect after an hour with the same IP. Thanks for your help! – Anggrayudi H Feb 28 '15 at 07:13
1

Such links redirect browsers to the actual content using HTTP redirects. To get the correct content type, all you have to do is tell HttpURLConnection to follow the redirects by setting setFollowRedirects() to true (documented here).

Malt
  • 28,965
  • 9
  • 65
  • 105
0

MimeTypeMap.getFileExtensionFromUrl(url)

Karan Datwani
  • 532
  • 6
  • 6
0

This one worked for me, you have to use retrofit to check the headers of response. First you have to define an endpoint to call it with the url you want to check:

@GET
suspend fun getContentType(@Url url: String): Response<Unit>

Then you call it like this to get the content type header:

api.getContentType(url).headers()["content-type"]
Astrit Veliu
  • 1,252
  • 1
  • 12
  • 22