我有一个应用需要根据来自 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
我只有以下代码来将 String
解析为 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;字符集=utf-8
;
对于 zip - application/zip
关于java - 检查来自 URL : Is it a file or webpage? 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27978778/