Hallo, ich versuch eine Datei herunterzuladen. Das geht zwar, aber wenn ich vorher das timeout umstelle, geht es nicht mehr. Bei Standarttimeout geht es, dieses ist aber null und man kann null nicht als Property setzen, weil man sonst eine nullPointerException bekommt. Ein leerer String ("") geht auch nicht. Wie krieg ich das jetzt hin? Ich hab auch schon versucht die default Properties zu speichern und dann einzusetzen, gibt aber immer nocht den Fehler. Hier ist mein Code:
Error:
Java:
public void downloadFile(String url, String outputPath) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
connection.connect();
int size = connection.getContentLength();
File file = new File(outputPath);
FileOutputStream fout = new FileOutputStream(file);
InputStream stream = connection.getInputStream();
int downloaded = 0;
while (true) {
byte buffer[];
buffer = new byte[size];
int read = stream.read(buffer);
if (read == -1)
break;
fout.write(buffer, 0, read);
downloaded += read;
System.out.println((downloaded / size) + "% (" + downloaded
+ "/" + size + ")");
}
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Error:
Code:
java.net.SocketTimeoutException: Read timed out
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at downloadFile(Codebeispiel:13)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
at java.net.URLConnection.getHeaderFieldInt(Unknown Source)
at java.net.URLConnection.getContentLength(Unknown Source)
at downloadFile(Codebeispiel:8)
... 1 more
Zuletzt bearbeitet: