Downloading files and verifying MD5 checksums
So this code works for 95% of people who use my application but sometimes the file seems to partially corrupt or whatever as it claims the MD5 checksum is different and then tries downloading again. It only really seems to happen with slow connections but the file downloads fine if they open up their web browser and download it directly.
Any suggestions?
PHP Code:
public void update(boolean hasInstallDirectory) {
for (String nextMirror : mirrors) {
try {
jout("Attempting to download...");
java.net.URLConnection updateConnection = null;
try {
updateConnection = new java.net.URL(nextMirror).openConnection();
} catch (MalformedURLException e1) {
jout("Mirror down: " + mirrors.indexOf(nextMirror) + 1);
} catch (IOException e1) {
jout("Mirror down: " + mirrors.indexOf(nextMirror) + 1);
}
int l = updateConnection.getContentLength();
byte[] arrayOfByte = new byte[l];
java.io.BufferedInputStream localBufferedInputStream = null;
try {
localBufferedInputStream = new java.io.BufferedInputStream(updateConnection.getInputStream());
} catch (IOException e1) {
jout("An error has occured while reading from the mirror...");
jout("Please post a new thread in the support section of the forum.");
}
int i1 = 0;
int i2 = 0;
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
jProgressBar1.setStringPainted(true);
while (i2 < l) {
try {
i1 = localBufferedInputStream.read(arrayOfByte, i2, arrayOfByte.length - i2);
} catch (IOException e) {
jout("An error has occured while reading from the mirror...");
jout("Please post a new thread in the support section of the forum.");
}
if (i1 == -1)
break;
i2 += i1;
jProgressBar1.setValue((int)(((float)i2 / (float)l) * 100F));
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
java.io.FileOutputStream localFileOutputStream = null;
try {
localFileOutputStream = new java.io.FileOutputStream(JAR);
} catch (FileNotFoundException e1) {
jout("An error has occured while opening the file output stream...");
jout("Please post a new thread in the support section of the forum.");
}
try {
localFileOutputStream.write(arrayOfByte);
} catch (IOException e) {
jout("An error has occured writing to disk...");
jout("Please post a new thread in the support section of the forum.");
e.printStackTrace();
}
try {
localFileOutputStream.flush();
localFileOutputStream.close();
localBufferedInputStream.close();
} catch (IOException e) {
jout("Error finalizing streams...");
jout("Please post a new thread in the support section of the forum.");
e.printStackTrace();
}
if (!requiresUpdate(JAR)) {
unpack();
jButton1.setEnabled(true);
} else {
jout("Hash mismatch after download! Retrying...");
update(true);
}
break;
} catch(Exception e) {
jout("Generic Exception: " + e.getMessage());
continue;
}
}
}
Re: Downloading files and verifying MD5 checksums
Re: Downloading files and verifying MD5 checksums
I might be the only helper on the forum right now, and I'm actually looking at going to sleep. The bad news is that I don't actually have a grasp of the URL Connect and that sort of stuff. However, there are plenty of people on the forum that do and they will help you. Wait about 24 hours, as most of the helpers seem to come on around the same time, and that isnt for another 10-12 hours at the earliest. Regardless, I will be watching this post as I'm interested in learning about URL Connect and that sort of crap.
Re: Downloading files and verifying MD5 checksums
I'm not sure this is necessarily a problem with the code as it is the connection, especially since this works with 95% of users. As per the BufferedReader api, if the ready() method returns false the read will return, which could result in an unfinished read. You could try skipping the BufferedReader, and just use Reader read function.