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

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

问题描述

我收到异常:URI 方案不是文件"

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

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

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.

我有一个名为url"的字符串,这里是我的代码:

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

导致错误的那一行是:

File fileFromUri = new File(fileUri);                   

我已经添加了其余的代码,所以你可以看到我想要做什么.

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

推荐答案

URI "scheme" 是 ":" 之前的东西,例如 "http://stackoverflow.com".

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

错误消息告诉您 new File(fileUri) 仅适用于file:"URI(指当前系统上的路径名),而不适用于其他方案,如http".

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".

基本上,file:"URI 是另一种指定File 类路径名的方式.这不是告诉 File 使用 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天全站免登陆