Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating URLs

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I have codes that sets a URL and pass it onto Image myImage= new Image(myURL);

The problem is there doesn't seem to be any validation of the URL (i.e. the file actually exists at the stated URL) with the URL' constructor.

I even manage to call myURL.openConnection.connect() with no exceptions thrown at all.

What do I need to do to validate the existence of a file? FYI I am using merely the file protocol
 
A little test code:
Code:
public class TestURL {

    public static void main( String[] args ) {

        try {
            URL good = new URL( "[URL unfurl="true"]http://www.tek-tips.com"[/URL] );
            good.openConnection().connect();
            URL hmm  = new URL( "[URL unfurl="true"]http://doesnt.really.exist"[/URL] );
            hmm.openConnection().connect();
            URL bad  = new URL( "xyz" );
            bad.openConnection().connect();
        } catch( Exception e ) {
            System.out.println( "Bad URL: " + e.getMessage() );
            e.printStackTrace();
        }
    }
}
Falls over with:
Code:
Bad URL: doesnt.really.exist
java.net.UnknownHostException: doesnt.really.exist
        etc...
So it looks like its working to me. Perhaps if you show us the URL you are trying to reach?
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top