Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extending the default file URL protocol handler to work with Window shares?

Thread Tools

Extending the default file URL protocol handler to work with Window shares?

Filip Larsen
Posts: n/a
I have been trying to use the URL class in an application for specifying
files on a Window share (with syntax "\\host\path\file"), but without much

I am using some guesswork here since the two classes in play, Handler and
FileURLConnection, both are in the package to
which I don't have any source code, but the following seems to happen:

- a file Handler singleton is installed when the first file protocol URL is
- an url is made such that url.toString().equals("file:\\host\path\file"),
- the url gets an openConnection call,
- the url delegates the openConnection method call the Handler singleton,
- the Handler singleton checks if File("\path\file") exists, which it
- the Handler falls back trying the URL "ftp://host/path/file" instead,
which also fails.

So, the problem as I see it is that the file Handler is stripping the
hostname away from the string it gives File and I have not been able to find
any URL syntax for which this doesn't happen.

Since the File class have no problems with the "\\host\path\file" notation I
then tried to install a very simple file protocol handler with

public class MyURLHandlerFactory implements URLStreamHandlerFactory {
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("file".equals(protocol)) return new MyFileURLStreamHandler();
return null;

public class MyFileURLStreamHandler extends URLStreamHandler {
protected URLConnection openConnection(URL u) throws IOException {
return new MyFileURLConnection(u,new File(u.getPath()));

public class MyFileURLConnection extends {
public MyFileURLConnection(URL u, File f) {

where the last class is necessary because only have one constructor which
is protected. Note that in my case, u.getPath().equals("\\host\path\file")
which gives a File that actually exists.

This handler almost works. It seems to work fine with the normal file URL's
that are used during application start-up and also with those special
windows-URL's I feed it. However, now the file URL's are no longer parsed
into a canonical form (like "file://host/path/file") so building new
relative URLs from those backslash-URLs do not work except for very simple
cases like new URL("file:\\host\path","file"). More complicated things like
new URL("file:\\host\path","../otherpath/file") ends up as new
File("\\host\path/../otherpath/file") which is not working.

One should think that the problem of getting Java URLs to support Windows
UNC paths must have been solved before by someone, but a quick visit to
google did not show up anything. So now, before I go spending a big effort
to research and write my own full-fledged FileURLConnection, I would like to
ask if any of you have any pointers on how I go about adding "general"
support for Windows UNC paths to the URL system?

And if anyone have the Java 1.4 source code for those two "unsupported" sun
classes mentioned above and are allowed to show it to others, I'd almost
give a body part to see it

Filip Larsen

Reply With Quote
Posts: n/a
Just an idea:
If you want to access \\host\foo\file, you have to use in Java:
File("\\\\host\\foo\\file") because the backslash will be interpreted, and
the result of the \\host\foo\file would be for java
\host(form-feed)oo(form-feed)ile. Backslash marks an escape character.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Protocol Chart - Learn how to use a Protocol Analyzer Wireless Networking 0 08-21-2004 04:35 PM
Windows URL protocol handler in python? Python 2 08-18-2004 06:50 PM
When i try to implement a server program giving UDP as protocol , it works fine , but if the same code is executed with TCP as protocol option, it gives an error. Tompyna Perl Misc 4 02-17-2004 06:51 PM
A protocol handler Ravi Shankar Java 0 01-12-2004 12:44 PM
Proper way to create a protocol handler Michael Conner Java 3 11-04-2003 03:42 PM