URI方案不是“文件” [英] URI scheme is not "file"

查看:183
本文介绍了URI方案不是“文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到例外:URI方案不是文件



我正在做的是尝试获取一个文件的名称,然后保存该文件(从我有一个名为url的字符串,这里是我的代码:

  url = Streams.asString(stream); //从网页上的表单获取URL 
System.out.println(这是URL:+ url);
URI fileUri = new URI(url);

文件fileFromUri = new File(fileUri);

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

文件newFolder = new File(getServletContext()。getRealPath(pics));
if(!newFolder.exists()){
newFolder.mkdir();
}
IOUtils.copy(imageStream,new FileOutputStream(pics /+ onlyFile));
}

导致错误的行是这样一个:

 文件FileFromUri = new File(fileUri); 

我已经添加了其余的代码,以便您可以看到我正在做的事情。 p>

解决方案

URIscheme是:)之前的东西,例如 http://stackoverflow.com



错误信息告诉你, new File(fileUri)只对file:URI有效(它指的是当前系统上的路径名),而不是像http这样的其他方案。



基本上,file:URI是另一种指定 File 类的路径名的方式。告诉文件以使用http从网络获取文件不是一种奇妙的方式。


I get the exception: "URI scheme is not file"

What I am doing is trying to get the name of a file and then save that file (from another server) onto my computer/server from within a servlet.

I have a String called "url", from thereon here is my code:

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

The line causing the error is this one:

File fileFromUri = new File(fileUri);                   

I have added the rest of the code so you can see what I am trying to do.

解决方案

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.

这篇关于URI方案不是“文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆