Java URI方案不是;文件";

Java URI方案不是;文件";,java,exception,servlets,uri,file-uri,Java,Exception,Servlets,Uri,File Uri,我得到一个异常:“URI方案不是文件” 我正在做的是尝试获取文件名,然后将该文件(从另一台服务器)从servlet中保存到我的计算机/服务器上 我有一个名为“url”的字符串,下面是我的代码: url = Streams.asString(stream); //gets the URL from a form on a webpage System.out.println("This is the URL: "+url); URI fileUri = new URI(url); File fi

我得到一个异常:“URI方案不是文件”

我正在做的是尝试获取文件名,然后将该文件(从另一台服务器)从servlet中保存到我的计算机/服务器上

我有一个名为“url”的字符串,下面是我的代码:

url = Streams.asString(stream); //gets the URL from a form on a webpage
System.out.println("This is the URL: "+url);
URI fileUri = new URI(url);

File fileFromUri = new File(fileUri);                   

onlyFile = fileFromUri.getName(); 
URL fileUrl = new URL(url);
InputStream imageStream = fileUrl.openStream();
String fileLoc2 = getServletContext().getRealPath("pics/"+onlyFile);

File newFolder = new File(getServletContext().getRealPath("pics"));
    if(!newFolder.exists()){
        newFolder.mkdir();
    }
    IOUtils.copy(imageStream, new FileOutputStream("pics/"+onlyFile));
} 
导致错误的行如下所示:

File fileFromUri = new File(fileUri);                   
我已经添加了代码的其余部分,因此您可以看到我正在尝试执行的操作。

URI“scheme”是位于“:”之前的内容,例如“”中的“http”

错误消息告诉您,
newfile(fileUri)
仅对“File:”URI(指当前系统上的路径名)有效,而不是像“http”这样的其他方案


基本上,“file:”URI是为
文件
类指定路径名的另一种方式。告诉
文件
使用http从web获取文件并不是一种神奇的方法。

您假设从
URL
创建
文件
在这里是错误的

您不需要创建一个
文件
从URL到Internet中的文件,这样您就可以获得文件名

您只需像这样解析URL即可:

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);

那么,如何使用FileReader使用HTTP获取文件呢?