URI scheme is not “file”

懵懂的女人 提交于 2019-11-28 12:00:33

The URI "scheme" is the thing that comes before the ":", for example "http" in "http://stackoverflow.com".

The error message is telling you that new File(fileUri) works only on "file:" URI's (ones referring to a pathname on the current system), not other schemes like "http".

Basically, the "file:" URI is another way of specifying a pathname to the File class. It is not a magic way of telling File to use http to fetch a file from the web.

Your assumption to create File from URL is wrong here.

You just don't need to create a File from URL to the file in the Internet, so that you get the file name.

You can simply do this with parsing the URL like that:

URL fileUri = new URL("http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/domefisheye/ladybug/fish4.jpg");    
int startIndex = fileUri.toString().lastIndexOf('/');
String fileName = fileUri.toString().substring(startIndex + 1);
System.out.println(fileName);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!