检查URL中的内容:是文件还是网页? [英] Checking the content from an URL: Is it a file or webpage?
问题描述
我的应用需要根据网址
中的内容采取不同的操作。如果内容是文件,我需要下载它。但是,如果内容是网页,我需要打开它。据我所知,有两种网址类型:
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:
- 直接链接。例如: https://dl-ssl.google.com/android /repository/android-14_r04.zip
- 非直接链接。例如: http://www.example.com/downloadfile?file=12345
- 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
以及网页示例: https://stackoverflow.com/questions/xxxxxx/how-to-check-a-url-contain
我只有以下代码来解析字符串
到 URL
:
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);
所以,我的问题:
-
如何查看内容?是文件还是网页?
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?
提前致谢。
推荐答案
你可以查看内容类型:
URLConnection c = url.openConnection();
String contentType = c.getContentType();
for html - text / html;字符集= UTF-8
;邮编
- 申请/邮编
for html - text/html; charset=utf-8
;
for zip - application/zip
这篇关于检查URL中的内容:是文件还是网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!