2012-03-23 38 views
4

我有一个使用文件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问题或者是它对我如何工作的一个很大的误解。

+3

你正在运行在sanity(UNIX风格的路径)和疯狂(Windows文件共享路径)之间的接口中的crufty bits – 2012-03-23 01:20:06

+0

我刚刚发现另一个[问题](http://stackoverflow.com/questions/ 1878024/file-uris-and-slashes)类似于这个问题。 – Lance 2012-03-23 01:31:51

回答

0

也许'file://remotemachine/my/path/data.dat'?两个斜线,而不是四个。