[java/URL] URL Existenzprüfung

TommyMo

Erfahrenes Mitglied
Hi Leute!

Ich würde gerne in meiner Applikation mit URLs arbeiten. Da es sein kann, dass ich auch ein lokales File als URL angebe oder auch eine x-beliebig andere Quelle, möchte ich vor dem Zugriff auf dieses Ressource prüfen ob die Ressource überhaupt vorhanden ist.

Gibt es für die URL Klasse eine Möglichkeit dies abzuprüfen? Ich habe diesbezüglich nur die Klasse UMLConnection gefunden. Die realisiert aber nicht wirklich das was ich brauchen würde. Im Forum selbst habe ich zu dem Thema auch noch nichts gefunden.

Für Tipps wär ich dankbar.

Gruß
TOM
 
Ok, denke die Lösung liegt doch so nahe. Hier meine Realisierung:

Code:
...
try {
        // anlegen eines neuen URLConnection Objektes und öffnen einer Verbindung ...  
	URLConnection conn = modelURL.openConnection();
	conn.connect();
	...
}
catch (Exception e) {
	// handle your exception properly ...
}
...

Sollte es einen schöneren Weg geben, bitte melden!

Gruß
TOM
 
Hallo!

Wie waers denn damit?
Code:
 /**
  * 
  */
 package de.tutorials;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.net.URL;
 
 /**
  * @author daritho
  * 
  */
 public class URLAvailabilityCheck {
 
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) throws Exception {
 		URL url = new URL("http://www.tutsorials.de");
 		System.out.println(isURLAvailable(url));
 
 		File file = new File("c:/windowss");
 		System.out.println(isURLAvailable(file.toURL()));
 
 	}
 
 	private static boolean isURLAvailable(URL url) throws URISyntaxException {
 
 		if (url.getProtocol().equalsIgnoreCase("file")) {
 			File file = new File(url.toURI());
 			return file.exists();
 		} else if (url.getProtocol().equalsIgnoreCase("http")) {
 			InputStream inputStream = null;
 			try {
 				inputStream = url.openStream();
 				return true;
 			} catch (IOException ioe) {
 				ioe.printStackTrace();
 			} finally {
 				if (inputStream != null) {
 					try {
 					    inputStream.close();
 				    } catch (IOException e) {
 					    e.printStackTrace();
 					}
 				}
 			}
 		}
 		return false;
 	}
 
 }
...schaut aber leider auch nicht viel besser aus. Na ja, den Hostname koennte man noch mit InetAddress.getByName("www.tutorials.de").isReachable(100) auf existenz pruefen, aber um einen Pfad, bzw eine Datei nachzuweisen bleibt wohl das oeffnen und schliessen eines Streams die einzige(?) Moeglichkeit...

Gruss Tom
 
Hi Tom!

Laut JavaDOC öffnet der Befehl

Code:
URLConnection conn = myURL.openConnection();

von vornherein eine Connection zu dieser Ressource. Ich habe es noch nicht ausprobiert, aber eigentlich sollte das obere Statement reichen, also für Web- und Fileressourcen.

Gruß
TOM
 
Hallo!

...ich denke das allein reicht nicht aus.
Siehe:
Code:
/**
 * 
 */
package de.tutorials;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author daritho
 * 
 */
public class URLAvailabilityChecker {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://www.google.de/foo/bar/someImage.jpg");
		File file = new File("c:/windowss");

		System.out.println("isURLAvailable: ");
		System.out.println(isURLAvailable(url));
		System.out.println(isURLAvailable(file.toURL()));
		System.out.println("faultyIsURLAvailable");
		System.out.println(faultyIsURLAvailable(url));
		System.out.println(faultyIsURLAvailable(file.toURL()));

	}

	private static boolean faultyIsURLAvailable(URL url) {
		URLConnection connection;
		try {
			connection = url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private static boolean isURLAvailable(URL url) throws URISyntaxException {

		if (url.getProtocol().equalsIgnoreCase("file")) {
			File file = new File(url.toURI());
			return file.exists();
		} else if (url.getProtocol().equalsIgnoreCase("http")) {
			InputStream inputStream = null;
			try {
				inputStream = url.openStream();
				return true;
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return false;
	}

}
URLConnection conn = myURL.openConnection(); ruft nur dann die Feuerwehr, wenn der Host nicht gefunden werden kann. Wird der Host gefunden, kann man als URL Pfad alles moegliche Angeben... deshalb denke ich das man das doch auf die gezeigte ausfuehrliche Art pruefen sollte...

Gruss Tom
 
Zurück