![](/img/trans.png)
[英]java.net.URISyntaxException when downloading with DefaultHttpClient
[英]java.net.URISyntaxException
我得到了這個例外。 但此異常不會再次重現。 我想知道原因
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.
為什么會出現這個異常。 怎么處理才不會重現。
有效的URI不包含反斜杠,如果它包含了一個:
那么之前第一個字符:
必須是一個“協議”。
基本上"C:\\Documents and Settings\\All Users\\.SF\\config\\sd.xml"
是一個路徑名,而不是一個有效的URI。
如果要將路徑名轉換為“文件:”URI,請執行以下操作:
File f = new File("C:\Documents and Settings\All Users\.SF\config\sd.xml");
URI u = f.toURI();
這是在 Java 中將路徑名轉換為有效 URI 的最簡單、最可靠和可移植的方法。 它應該適用於 Windows、Mac、Linux 和任何其他支持 Java 的平台。 (其他涉及在路徑名上使用字符串攻擊的解決方案不可移植。)
但是您需要意識到“file:”URI 有許多警告,如File.toURI()
方法的 javadoc 中所述。 例如,在一台機器上創建的“文件:”URI 通常表示另一台機器上的不同資源(或根本沒有資源)。
其根本原因是文件路徑包含 Windows 中的正斜杠而不是反斜杠。
嘗試這樣解決問題:
"file:" + string.replace("\\", "/");
你必須有這樣的字符串:
String windowsPath = file:/C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
通常,人們會做這樣的事情:
String windowsPath = file:C:/Users/sizu/myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
或類似的東西:
String windowsPath = file:C:\Users\sizu\myFile.txt;
URI uri = new URI(windowsPath);
File file = new File(uri);
它需要一個帶有類型/協議的完整 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.
我寧願使用直接字符串來避免創建額外的文件對象。
在將命令行上的 URI 傳遞給腳本時,我遇到了相同的“不透明”錯誤。 這是在窗戶上。 我必須使用正斜杠,而不是反斜杠。 這為我解決了它。
我有這個例外。 但是不會再次復制此異常。 我想得到這個原因
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.
為什么發生此異常。 如何處理,所以它不會重現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.