检查URL中的内容:是文件还是网页?

我有一个应用程序需要根据URL的内容采取不同的操作。 如果内容是文件,我需要下载它。 但是,如果内容是网页,我需要打开它。 据我所知,有两种URL类型:

  • 直接链接 。 例如: https : //dl-ssl.google.com/android/repository/android-14_r04.zip
  • 直接链接 。 例如: http : //www.example.com/downloadfile?file = 12345

以及网页示例: https : //stackoverflow.com/questions/xxxxxx/how-to-check-a-url-contain

我只有以下代码来解析StringURL

 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(); 

对于html – text/html; charset=utf-8 text/html; charset=utf-8 ; 用于拉链 – application/zip

请允许我添加我的答案虽然这个问题是一年之久。

Android提供了一个URL实用程序,可以根据URL检查许多选项。 尝试:

  URLUtil.isFileUrl(URL) 

其他选项包括isDataUrl,isContentUrl等。

谢谢