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;
}
}
}