Hallo Leutz.
hab folgendes Problem:
ich will eine Datei deren URL ich habe downloaden. Die Datei wird erstellt auch mit richtiger Endung und der Name stimmt ebenfalls. Aber die Datein sind permanent 0kb groß und leer. Ich weiß aber nicht warum.
Hier mein Code:
danke schonmal
hab folgendes Problem:
ich will eine Datei deren URL ich habe downloaden. Die Datei wird erstellt auch mit richtiger Endung und der Name stimmt ebenfalls. Aber die Datein sind permanent 0kb groß und leer. Ich weiß aber nicht warum.
Hier mein Code:
Java:
public static void downloadFile(URL url, FileOutputStream os)
throws IOException {
if (url == null)
return;
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
byte tmp_buffer[] = new byte[4096];
InputStream is = conn.getInputStream();
int len;
while ( (len = is.read(tmp_buffer)) != -1) {
os.write(tmp_buffer, len, 0);
}
os.flush();
} else {
throw new IllegalStateException("HTTP Response: " + responseCode);
}
}
danke schonmal