A
AdamMyth
Gast
Hallo,
ich möchte eine Datei von einem HTTP-Server downloaden. Den nötigen Source-Code habe ich mir bereits zusammengebastelt. Siehe hier:
Nun, das mit dem runterladen klappt bereits problemlos. Doch ich habe Probleme mit den Statistiken zu dem Download. Denn ich will später eine Aussage machen, wie "129 kb/s", doch irgendwie geht das nicht ....
Könnt Ihr mir sagen, was ich falsch mache und wie ich das richtig mache??? Bittte ...
Gruß,
Adam
ich möchte eine Datei von einem HTTP-Server downloaden. Den nötigen Source-Code habe ich mir bereits zusammengebastelt. Siehe hier:
Java:
URL url = new URL("http://downloads.sourceforge.net/project/virtualboximage/OpenSolaris/2008.05/opensolaris-2008.05-x86.7z?use_mirror=freefr");
URLConnection uc = url.openConnection();
uc.connect();
long totalread = 0, contentLength = uc.getContentLength();
FileOutputStream out = new FileOutputStream(new File("C:/out.dat"));
InputStream in = uc.getInputStream();
int BUF_SIZE = 1 << 8, bytesRead = -1;
byte[] buffer = new byte[BUF_SIZE];
System.out.println("Laenge: " + contentLength);
long last = -1;
long down = -1;
while ((bytesRead = in.read(buffer)) > -1) {
out.write(buffer, 0, bytesRead);
totalread += bytesRead;
if (last < 0 && down < 0) {
last = System.currentTimeMillis();
down += bytesRead;
} else {
long diff = System.currentTimeMillis() - last;
if (diff >= 1000) {
last = -1;
System.out.println(down + " in " + diff);
System.out.println((totalread/contentLength)*100 + "%");
System.out.println(down/diff);
System.out.println("----------");
down = -1;
}
}
}
in.close();
out.close();
Nun, das mit dem runterladen klappt bereits problemlos. Doch ich habe Probleme mit den Statistiken zu dem Download. Denn ich will später eine Aussage machen, wie "129 kb/s", doch irgendwie geht das nicht ....
Könnt Ihr mir sagen, was ich falsch mache und wie ich das richtig mache??? Bittte ...
Gruß,
Adam