Java Properties, Downloader

  • Themenstarter Themenstarter GsT
  • Beginndatum Beginndatum
G

GsT

Gast
Hi!

Ich habe nun mit der Java Properties Klasse gearbeitet - Allerdings weiß ich nicht, wie ich die Properties quasi speichern soll.

Java:
File f = new File("Config.properties");
	
	public void load(){
		try {
			p.load(new FileInputStream(f));
		} catch (FileNotFoundException e) {
			try {
				f.createNewFile();
				p.load(new FileInputStream(f));
			} catch (IOException e1) {}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		p.setProperty("Iwas", "iwas");
		
		Iwas 		= p.getProperty("Iwas");

	}
Selbst wenn ich per Hand Iwas=iwasanderes in die Date ischreibe, benutzt er das was ich per Code gesettet habe.. Obwohl er das nur benutzen soll, wenn nichts anderes vorhanden ist.

Zweites Problem:

Java:
    public static void downloadFile(String url_str, OutputStream os)  throws IllegalStateException, MalformedURLException, ProtocolException, IOException { 

    	URL url = new URL(url_str.replace(" ", "%20")); 

    	HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    	
    	conn.setRequestMethod("GET"); 

    	conn.connect(); 

    	int responseCode = conn.getResponseCode(); 

    	if (responseCode == HttpURLConnection.HTTP_OK) { 
    		System.out.println(Prop.download.replaceAll("%name", url_str));
    		byte tmp_buffer[] = new byte[4096]; 

    		InputStream is = conn.getInputStream(); 

    		int n; 

    		while ((n = is.read(tmp_buffer)) > 0) { 
    			os.write(tmp_buffer, 0, n); 
    			os.flush(); 
    		} 

    	} else { 
    		System.out.println(Prop.DL_ERR.replaceAll("%name", url_str).replaceAll("%html_response", "" + responseCode));
    	} 
    }
Sollte bei Fehlern eigentlich einen String ausgeben - Gibt aber stattdessen eine Fehlermeldung, immer wenn es timeout ist.
 
Allerdings weiß ich nicht, wie ich die Properties quasi speichern soll.
Hier steht's beschrieben: Properties (Java Platform SE 7 )
Und hier auch: Properties (The Java™ Tutorials > Essential Classes > The Platform Environment)
Selbst wenn ich per Hand Iwas=iwasanderes in die Date ischreibe, benutzt er das was ich per Code gesettet habe.. Obwohl er das nur benutzen soll, wenn nichts anderes vorhanden ist.
Das setze die Defaults vor dem Laden, nicht danach...

Zweites Problem: Sollte bei Fehlern eigentlich einen String ausgeben - Gibt aber stattdessen eine Fehlermeldung, immer wenn es timeout ist.
Und welche Fehlermeldung ist das?

bye
TT
 
Irgendwie verstehe ich es gerade nicht =S
Was muss ich daran:
Java:
	public void load(){

		p.setProperty("Datei-Format", "Bild_%tid-%page_");
		try {
			p.store(new FileOutputStream(f), "");
			p.load(new FileInputStream(f));
		} catch (FileNotFoundException e) {
			try {
				f.createNewFile();
				p.load(new FileInputStream(f));
			} catch (IOException e1) {}
		} catch (IOException e) {e.printStackTrace();}
		
		fileformat	= p.getProperty("Datei-Format");
	}
ändern, damit es richtig lädt? Wenn ich es vor dem setten lade, aber die Datei keinen Inhalt hat, ist es ja auch sinnlos :/

Die Fehlermeldung:

Java:
java.net.ConnectException: Connection timed out: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(Unknown Source)
	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at sun.net.NetworkClient.doConnect(Unknown Source)
	at sun.net.[url]www.http.HttpClient.openServer(Unknown[/url] Source)
	at sun.net.[url]www.http.HttpClient.openServer(Unknown[/url] Source)
	at sun.net.www.http.HttpClient.<init>(Unknown Source)
	at sun.net.[url]www.http.HttpClient.New(Unknown[/url] Source)
	at sun.net.[url]www.http.HttpClient.New(Unknown[/url] Source)
	at sun.net.[url]www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown[/url] Source)
	at sun.net.[url]www.protocol.http.HttpURLConnection.plainConnect(Unknown[/url] Source)
	at sun.net.[url]www.protocol.http.HttpURLConnection.connect(Unknown[/url] Source)
	at dl.Downloader.downloadFile(Downloader.java:28)
	at dl.Downloader.<init>(Downloader.java:95)
	at dl.Downloader.<init>(Downloader.java:79)
	at dl.Downloader.main(Downloader.java:56)
 
Properties: Gelöst 😀
Connection-Problem:
Jap. Nach der Rang-Ordnung in der Fehlermeldung sortiert:
conn.connect();
downloadFile(ss, new FileOutputStream(new File(datei)));
}catch(Exception ee){System.out.println(MSG.error);new Downloader();}
new Downloader();

Aber nur bei Time-Out. Gibt es eine Möglichkeit den Time-Out zu verkürzen, und abzufangen?
 

Zurück
Oben