Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从URL中提取文件名_Java_File_Extension Methods - Fatal编程技术网

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