Checking the content from an URL: Is it a file or webpage?

£可爱£侵袭症+ 提交于 2019-12-06 04:52:52

问题


I have an app which need different action based on the content from an URL. If the content is a file, I need to download it. But, if the content is a webpage, I need to open it. As far I know, there are two URL types:

  • Direct link. For example: https://dl-ssl.google.com/android/repository/android-14_r04.zip
  • Undirect link. For example: http://www.example.com/downloadfile?file=12345

And the example for webpage: https://stackoverflow.com/questions/xxxxxx/how-to-check-a-url-contain

I only have the following code to parse a String to URL:

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

So, my questions:

  • How can I check the content? Is it a file or webpage?

  • If it's a file, how can I check the content from undirect link?

Thanks in advance.


回答1:


you can check content type:

    URLConnection c = url.openConnection();
    String contentType = c.getContentType();

for html - text/html; charset=utf-8; for zip - application/zip




回答2:


Permit me to add my answer though this question is a year old.

Android provides a URL utility that can check many options based on URL. Try:

        URLUtil.isFileUrl(URL)

Other options includes isDataUrl, isContentUrl etc.

Thanks



来源:https://stackoverflow.com/questions/27978778/checking-the-content-from-an-url-is-it-a-file-or-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!