Hallo,
ich habe folgendes Problem :
ich habe in Java ein Download-Objekt welches byteweise eine Datei von einen Server kopiert.
das ganze Funkioniert auch zu 99,9%, nur der Download schließt nicht ab, sprich er bleibt bei 100-700 KB vor der Beendigung stehen und wertet den Download als beendet. Ich finde dabei absolut keinen Lösungsansatz.
hier der Code für das Objekt:
Dabei lass ich mir immer die gedownloadete Größe, die Insgesammte Größe, die Differenz, den Status und die länge des byte Arrays ausgeben.
Dabei geschieht es jedoch, dass ich auch auf solche Ausgaben stoße:
zur näheren Erläuterung:
STATUS 0 = downloading
1 = paused
2 = complete
3 = cancelled
4 = error
ich habe folgendes Problem :
ich habe in Java ein Download-Objekt welches byteweise eine Datei von einen Server kopiert.
das ganze Funkioniert auch zu 99,9%, nur der Download schließt nicht ab, sprich er bleibt bei 100-700 KB vor der Beendigung stehen und wertet den Download als beendet. Ich finde dabei absolut keinen Lösungsansatz.
hier der Code für das Objekt:
Java:
public void run() {
RandomAccessFile file = null;
InputStream stream = null;
try {
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// Connect to server.
connection.connect();
if (debug) {
System.out.println(connection.getURL().toString());
System.out.println("\n-\n" + connection.getPermission());
System.out.println("\n-\n" + connection.getResponseCode());
System.out.println("\n-\n" + connection.getContentLength());
}
// Make sure response code is in the 200 range.
if (connection.getResponseCode() / 100 != 2) {
throw new Exception(this.name + " - code is not in the 200 range");
}
;
// Check for valid content length.
int contentLength = connection.getContentLength();
if (contentLength < 1) {
throw new Exception(connection.getContentLength() + "not a valid content length");
}
/*
* Set the size for this download if it hasn't been already set.
*/
if (size == -1) {
size = contentLength;
stateChanged();
}
// Open file and seek to the end of it.
file = new RandomAccessFile(this.pfad + "/" + this.name + ".zip", "rw");
file.seek(downloaded);
this.file = file;
stream = connection.getInputStream();
while (status == DOWNLOADING) {
//System.out.println("downloading");
/*
* Size buffer according to how much of the file is left to
* download.
*/
if((size-downloaded) <= 0) {
break;
}
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else{
buffer = new byte[size - downloaded];
}
System.out.println("-------------------------------------");
System.out.println("Status : " + status);
System.out.println("Size : " + size);
System.out.println("downloaded : " + downloaded);
System.out.println("difference : " + (size - downloaded));
System.out.println("Buffer : " + buffer.length);
System.out.println("-------------------------------------");
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;
// Write buffer to file.
if (new File(this.pfad + "/" + this.name + ".zip").exists()) {
file.write(buffer, 0, read);
downloaded += read;
}
stateChanged();
}
/*
* Change status to complete if this point was reached because
* downloading has finished.
*/
if (status == DOWNLOADING) {
status = COMPLETE;
System.out.println("finished");
//log.write2log(new String[] { "Download", "Fertig: download = " + this.name + " nach " + this.pfad + //"abgeschlossen.", this.name });
stateChanged();
}
} catch (Exception e) {
error(e);
if (debug) {
e.printStackTrace();
}
} finally {
// Close file.
if (file != null) {
try {
file.close();
} catch (Exception e) {
if (debug) {
e.printStackTrace();
}
}
}
// Close connection to server.
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
error(e);
if (debug) {
e.printStackTrace();
}
}
}
}
}
Dabei lass ich mir immer die gedownloadete Größe, die Insgesammte Größe, die Differenz, den Status und die länge des byte Arrays ausgeben.
Dabei geschieht es jedoch, dass ich auch auf solche Ausgaben stoße:
Code:
----------------------------
Status : 2
Size : 99785855
downloaded : 99787791
difference : -1936
Buffer : 1024
----------------------------
STATUS 0 = downloading
1 = paused
2 = complete
3 = cancelled
4 = error