文件URI在解析操作期间删除主机名路径 [英] File URI removing hostname path during a resolve operation
问题描述
我有一个资源已经通过使用文件URL从Windows网络上的网络共享加载而打开,例如文件:////remotemachine/my/path/spec.txt
。
I have a resource that has been opened by using a File URL to load from a network share on the Windows network, e.g. file:////remotemachine/my/path/spec.txt
.
此文件指定了我必须加载的另一个资源的路径。我使用 URI.resolve(String)
方法为此资源创建URI。这导致了一个问题,因为新创建的File资源不包含指示remotehost所必需的slahses。而不是
This file specifies a path to another resource that I have to load. I use the URI.resolve(String)
method to create a URI to this resource. This is causing a problem because the newly create File resource doesn't contain the necessary slahses to indicate the remotehost. Instead of
file:////remotemachine/my/path/data.dat
我得到
file:///remotemachine/my/path/data.dat
缺少的斜杠表示文件正在尝试加载来自资源不存在的本地机器(路径也没有)。
The missing slash means the file is trying to be loaded from the local machine where the resource doesn't exist (nor does the path).
如果我使用IP地址而不是机器名,这会做同样的事情。如果我使用映射文件名,例如 file:/// M:/path/spec.txt
然后资源文件正确解析为 file:/// M:/ path / data .DAT
。此外,如果我使用http协议路径,URI会正确解析。
This does the same thing if I use IP addresses instead of machine names. If I use a mapped file name, e.g. file:///M:/path/spec.txt
then the resource file correctly resolves to file:///M:/path/data.dat
. Also if I use a http protocol path the URI resolves correctly.
如果这是一个错误,任何人都可以确定我是否有再次解析文件URI的误解Java?
Can anyone identify if I have a misunderstanding in resolving File URIs again network shares of if this is a bug in 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();
}
更新
我已经找到了解决问题的方法
I've come up with a fix for my problem
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问题,或者只是对我如何工作的一个很大的误解。
However I'm still curious if the File: thing is a Java bug, a URI problem or just a big misunderstanding of how it works on my part.
推荐答案
也许'文件: //remotemachine/my/path/data.dat?两个斜线,而不是四个。
Maybe 'file://remotemachine/my/path/data.dat'? Two slashes, not four.
这篇关于文件URI在解析操作期间删除主机名路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!