gpt4 book ai didi

java - URI 方案不是 "file"

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:50 26 4
gpt4 key购买 nike

我得到异常:“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“方案”是“:”之前的内容,例如“http://stackoverflow.com”中的“http”。

错误消息告诉您 new File(fileUri) 仅适用于“文件:”URI(指当前系统上的路径名),不适用于“http”等其他方案。

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

关于java - URI 方案不是 "file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2843557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com