2011-02-14 73 views
28

我收到以下错误RAD:非法字符在索引路径16

java.net.URISyntaxException: Illegal character in path at index 16: file:/E:/Program Files/IBM/SDP/runtimes/base...... 

可否请你让我知道什么是错误以及如何解决它?

+1

参见[?如何解决路径此异常非法字符(http://stackoverflow.com/questions/3753852/how-解决这个异常非法字符在路径) – 2011-02-14 12:51:53

+1

尝试用'%20'替换空格 – nothrow 2011-02-14 12:52:05

+2

请参阅http://stackoverflow.com/questions/749709/how-to-deal-with上的一般解决方案-the-urisyntaxexception/15570670#15570670 – GKislin 2013-03-22 12:48:43

回答

42

索引16处有一个非法字符。我会说它不喜欢路径中的空间。您可以percent encode空格等特殊字符。在这种情况下用%20替换它。

我联系到上述建议采用URLEncoder问题:

String thePath = "file://E:/Program Files/IBM/SDP/runtimes/base"; 
thePath = URLEncoder.encode(thePath, "UTF-8"); 
+0

file:///是文件。注意三个 – 2012-05-11 16:04:40

+10

嗯......它并不真正的工作:空间被加号替换,而不是%20,并且所有的斜线也被破坏了...... 请参阅:http://stackoverflow.com/问题/ 4737841/urlencoder-not-translation-space-character – 2012-09-03 21:01:58

17

我跑进与必应地图API同样的事情。 URLEncoder只是让事情变得更糟,但replaceAll(" ","%20");伎俩。

1

安装目录不能有空间。 重装软件会纠正它

2

我有一个xml类似的问题。只是通过错误和解决方案(编辑Jonathon版本)。

代码:

HttpGet xmlGet = new HttpGet(xmlContent); 

xml格式:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<employee> 
    <code>CA</code> 
    <name>Cath</name> 
    <salary>300</salary> 
</employee> 

错误:

java.lang.IllegalArgumentException: Illegal character in path at index 0: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<contents> 
    <portalarea>CA</portalarea> 
    <portalsubarea>Cath</portalsubarea> 
    <direction>Navigator</direction> 
</contents> 
    at java.net.URI.create(URI.java:859) 
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69) 
    at de.vogella.jersey.first.Hello.validate(Hello.java:56) 

不完全完美的解决方案:(该实例消失错误)

String theXml = URLEncoder.encode(xmlContent, "UTF-8"); 
HttpGet xmlGet = new HttpGet(theXml); 

任何想法,我应该怎么做?它刚刚通过,但有问题,而这样做

HttpResponse response = httpclient.execute(xmlGet); 
5

你试试这个吗?

new File("<PATH OF YOUR FILE>").toURI().toString(); 
0

如果与使用JDK出现此错误是:

PROGRA〜1而不是在路径示例程序文件:

c:/progra~1/java instead of c:/program files/java 

这将是确定始终避免用Java代码空间.....

它可以用于程序文件中的所有东西,否则在开始处加上引号和路径的英文地址为

“c:/..../”

0

今天我得到了这个错误,不像以上所有的答案,我的错误是由于一个新的原因。

在我的日文翻译strings.xml文件中,我删除了必需的字符串。

一些android如何混淆所有其他字符串,并导致错误。

解决的办法是包括所有从我的正常琴弦,英语strings.xml中

包括那些没有翻译为日语字符串。

0

与空间有相同的问题。 URL和URI的组合解决了这个问题:

URL url = new URL("file:/E:/Program Files/IBM/SDP/runtimes/base"); 
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 

*资料来源:https://stackoverflow.com/a/749829/435605