Opening File URL in a Browser

I am trying to a TIFF file from my applet using file URL. The TIFF is located in the local file directory and I create an URL and get the Applet Context and call showDocument.

URL fileURL = new URL("file:///"+ <file location>);

applet.getAppletContext().showDocument(fileURL, "_blank");

The problem with the above code is that if the file location contains spaces it is not opening the TIFF file.

I tried using URLEncoder to encode the url so that the space could be replaced

URL fileURL = new URL("file:///"+ URLEncoder.encode(<file location>,"UTF-8"));

But when i tried with the above code, the spaces are converted to +. Even this is not recognized by browser.

What should I do so that spaces are converted to "%20" which is recognized by the browser. I tried various encoding formats nothing helps.

[864 byte] By [mvnveeraa] at [2007-11-26 16:10:02]
# 1

Rather than trying to figure out the file: url yourself you can rely on the

methods within the File and URI classes to perform the conversion

between file name and URL.import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;

public class FileUrlEg {

public static void main(String[] args) throws MalformedURLException {

File f = new File("space dir/walkaway.wav");

URL url = f.toURI().toURL();

System.out.println(url);

}

}

The output is:file:/C:/Documents%20and%20Settings/pbrockway2/My%20Documents/JDC/space%20dir/walkaway.wav

pbrockway2a at 2007-7-8 22:32:22 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank u very much for the solution. It worked fine.
mvnveeraa at 2007-7-8 22:32:22 > top of Java-index,Java Essentials,Java Programming...
# 3
You're welcome.
pbrockway2a at 2007-7-8 22:32:22 > top of Java-index,Java Essentials,Java Programming...