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
.
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
I get
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).
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.
Can anyone identify if I have a misunderstanding in resolving File URIs again network shares of if this is a bug in Java?
The relevant section of code
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();
}
Update
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());
}
}
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.