Java 从URL中提取文件名
例如Java 从URL中提取文件名,java,file,extension-methods,Java,File,Extension Methods,例如 如何从上面的URL中提取索引文件扩展名 尝试使用URL类的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 >
如何从上面的URL中提取索引文件扩展名 尝试使用URL类的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头中的内容类型,它将告诉您响应的类型。实际上,我正在尝试构建一个类似爬虫的应用程序,它将提取页面上的所有超链接,并以迭代方式访问每个已爬网的超链接fashion..可能的-1副本与Web API中使用的URL不兼容“”将返回“com/API/products”@JhonAlx,这不是URL。浏览器将此更改为
http://www.example.com/api/products%22
@JhonAlx这里有更多关于什么是维基文章中的URL的文档:一个典型的URL可能看起来像:@JhonAlx,如果我通过浏览器复制URL,它会说它是http://en.wikipedia.org/wiki/Main_Page