文件URI在解析操作期间删除主机名路径
我有一个资源已通过使用文件URL从Windows网络上的网络共享加载而打开,例如file:////remotemachine/my/path/spec.txt
。
此文件指定了我必须加载的另一个资源的路径。 我使用URI.resolve(String)
方法为此资源创建URI。 这导致了一个问题,因为新创建的File资源不包含指示remotehost所必需的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会正确解析。
如果这是Java中的错误,任何人都可以确定我是否有再次解析文件URI的误解?
相关的代码部分
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()); } }
但是我仍然很好奇,如果File:thing是一个Java bug,一个URI问题,或者只是对我如何工作的一个很大的误解。
也许’file://remotemachine/my/path/data.dat’? 两个斜线,而不是四个。