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

2024-01-19

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

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

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

我只有以下代码来解析String to URL:

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; 对于拉链 -application/zip

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查 URL 的内容:是文件还是网页? 的相关文章

随机推荐