I am downloading files with a Java Applet and running into problems with caching and file corruption.
I download files using a URLConnection by opening an InputStream to it, something like this
urlconnection = url.openConnection();
InputStream inputstream = getInputStream(currentFile, urlconnection);
FileOutputStream fos = new FileOutputStream(path + currentFile);
int bufferSize;
byte buffer[] = new byte[65536];
while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1) {
fos.write(buffer, 0, bufferSize);
...
}
inputstream.close();
fos.close();
On the rare occasion I download a file it turns out corrupt, I understand this is normal? (or is it that the buffer size is too big or something?), anyway what is annoying me is that on refreshing the applet the file should redownload right? but the file keeps turning out corrupt until the java cache is cleared from the java control panel. I’ve tried setting urlconnection.setUseCaches(false); and urlconnection.setDefaultUseCaches(false); but the problem still occurs, It still keeps downloading a corrupt file and only goes away when the java cache is cleared from the control panel. This leads me to believe that the file is still cached somewhere and is used on refresh instead of redownloading, it does seem like its redownloading though.
I am all out of idea’s now and not sure how or where its managing to cache the corrupt file (inputstream? browser cache?), any ideas or suggestions welcome.
thanks.