从 URL 中提取文件名

Extract file name from the URL

例如。 http://www.example.com/
如何从上面提取索引文件扩展名URL?

尝试使用 URL class

的 getpath 方法

我愿意

String url = "http://www.example.com/file.ext?query";
String file = new URL(url).getFile();
String lastFile = file.substring(file.lastIndex('/')+1);
int pos = lastFile.lastIndex('.');
String ext = pos > 0
           ? lastFile.substring(pos + 1) 
           : NONE;

注意:您的原始示例既没有文件也没有扩展名。

我相信您正在寻找 http headers 中的内容类型,它将告诉您响应的类型。

实际上我正在尝试构建一个类似 Crawler 的应用程序,它将提取页面上的所有超链接并以迭代方式访问每个已抓取的超链接..