java.net.URISyntaxException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2856126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.URISyntaxException
提问by aayushi soni
I have get this exception. but this exception is not reproduced again. I want to get the cause of this
我得到了这个例外。但此异常不会再次重现。我想知道原因
Exception Caught while Checking tag in XMLjava.net.URISyntaxException:
Illegal character in opaque part at index 2:
C:\Documents and Settings\All Users\.SF\config\sd.xml
stacktrace net.sf.saxon.trans.XPathException.
Why this exception occured. How to deal with so it will not reproduce.
为什么会出现这个异常。怎么处理才不会重现。
回答by Stephen C
A valid URI does not contain backslashes, and if it contains a :
then the characters before the first :
must be a "protocol".
有效的 URI 不包含反斜杠,如果它包含 a,:
则第一个之前的字符:
必须是“协议”。
Basically "C:\Documents and Settings\All Users\.SF\config\sd.xml"
is a pathname, and not a valid URI.
基本上"C:\Documents and Settings\All Users\.SF\config\sd.xml"
是一个路径名,而不是一个有效的 URI。
If you want to turn a pathname into a "file:" URI, then do the following:
如果要将路径名转换为“文件:”URI,请执行以下操作:
File f = new File("C:\Documents and Settings\All Users\.SF\config\sd.xml");
URI u = f.toURI();
This is the simplest, and most reliable and portable way to turn a pathname into a valid URI in Java. It should work on Windows, Mac, Linux and any other platform that supports Java. (Other solutions that involve using string bashing on a pathname are not portable.)
这是在 Java 中将路径名转换为有效 URI 的最简单、最可靠和可移植的方法。它应该适用于 Windows、Mac、Linux 和任何其他支持 Java 的平台。(其他涉及在路径名上使用字符串攻击的解决方案不可移植。)
But you need to realize that "file:" URIs have a number of caveats, as described in the javadocs for the File.toURI()
method. For example, a "file:" URI created on one machine usually denotes a different resource (or no resource at all) on another machine.
但是您需要意识到“文件:”URI 有许多注意事项,如该File.toURI()
方法的 javadoc 中所述。例如,在一台机器上创建的“文件:”URI 通常表示另一台机器上的不同资源(或根本没有资源)。
回答by sizu
You must have the string like so:
你必须有这样的字符串:
String windowsPath = file:/C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
Usually, people do something like this:
通常,人们会做这样的事情:
String windowsPath = file:C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
or something like this:
或类似的东西:
String windowsPath = file:C:\Users\sizu\myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
回答by britt
I had the same "opaque" error while passing a URI on the command line to a script. This was on windows. I had to use forward slashes, NOT backslashes. This resolved it for me.
在将命令行上的 URI 传递给脚本时,我遇到了相同的“不透明”错误。这是在窗户上。我必须使用正斜杠,而不是反斜杠。这为我解决了它。
回答by Ankireddy Polu
The root cause for this is file path contains the forward slashes instead of backward slashes in windows.
其根本原因是文件路径包含 Windows 中的正斜杠而不是反斜杠。
Try like this to resolve the problem:
尝试这样解决问题:
"file:" + string.replace("\", "/");
回答by sampopes
It needs a complete uri with type/protocol e.g
它需要一个带有类型/协议的完整 uri,例如
file:/C:/Users/Sumit/Desktop/s%20folder/SAMPLETEXT.txt
File file = new File("C:/Users/Sumit/Desktop/s folder/SAMPLETEXT.txt");
file.toURI();//This will return the same string for you.
I will rather use direct string to avoid creating extra file object.
我宁愿使用直接字符串来避免创建额外的文件对象。