我有一個使用文件URL從Windows網絡的網絡共享中加載的資源,例如, file:////remotemachine/my/path/spec.txt
。在解析操作中刪除主機名路徑的文件URI
該文件指定了我必須加載的另一個資源的路徑。我使用URI.resolve(String)
方法來創建此資源的URI。這是一個問題,因爲新創建的文件資源不包含必要的slahses來指示遠程主機。取而代之的
file:////remotemachine/my/path/data.dat
我得到
file:///remotemachine/my/path/data.dat
丟失的斜線意味着文件試圖從本地機器裝在資源不存在(也不路徑)。
如果我使用IP地址而不是機器名稱,這會做同樣的事情。如果我使用映射的文件名,例如file:///M:/path/spec.txt
然後資源文件正確解析爲file:///M:/path/data.dat
。另外,如果我使用http協議路徑,則URI可以正確解析。
任何人都可以識別,如果我有誤解再次解決File URIs網絡共享,如果這是一個Java中的錯誤?
的代碼中的相關部分
private Tile(URI documentBase, XPath x, Node n) throws XPathExpressionException, IOException
{
String imagePath = (String) x.evaluate("FileName", n, XPathConstants.STRING);
this.imageURL = documentBase.resolve(imagePath).toURL();
}
更新
,我想出了一個修復我的問題
private Tile(URI documentBase, XPath x, Node n) throws XPathExpressionException, IOException
{
boolean isRemoteHostFile = documentBase.getScheme().equals("file") &&
documentBase.getPath().startsWith("//");
String imagePath = (String) x.evaluate("FileName", n, XPathConstants.STRING);
imageURL = documentBase.resolve(imagePath).toURL();
if (isRemoteHostFile)
{
imageURL = new URL(imageURL.getProtocol()+":///"+imageURL.getPath());
}
}
不過我還是好奇,如果文件:這個東西是一個Java錯誤,一個URI問題或者是它對我如何工作的一個很大的誤解。
你正在運行在sanity(UNIX風格的路徑)和瘋狂(Windows文件共享路徑)之間的接口中的crufty bits – 2012-03-23 01:20:06
我剛剛發現另一個[問題](http://stackoverflow.com/questions/ 1878024/file-uris-and-slashes)類似於這個問題。 – Lance 2012-03-23 01:31:51