File download 1KB too large on large files
843841Jun 4 2003 — edited Jun 9 2003I am using the following code to download a file (a java application in an InstallAnywhere installer) and it works perfectly - EXCEPT that for large files (the file size is 21,992KB) it ends up with a downloaded file size of 21,993KB - 1KB too large! This of course means that the downloaded file (my application) won't run because the InstallAnywhere installer checks the file size before running so thinks the file is corrupted. I haved tested it many times and it is always 1KB too large. Small files download OK.
Any help would be most appreciated. Here is the code:
URL fileToDownloadsURL = this.getServletContext().getResource("/WEB-INF/Products/" + fileToDownloadsDirectoryURL + fileToDownloadsName);
File fileToDownload = new File(fileToDownloadsURL.getPath());
response.reset();
// attachments can be "inline" or "attachment"
//response.setHeader("Content-Disposition", "inline;filename=" + fileToDownloadsName + ";");
response.setHeader("Content-Disposition", "attachment;filename=" + fileToDownloadsName + ";");
if (fileToDownloadsName.indexOf('.') >= 0) {
if (fileToDownloadsName.indexOf('.') < fileToDownloadsName.length() - 1) {
String filesExtension = fileToDownloadsName.substring(fileToDownloadsName.indexOf('.') + 1);
try {
String mimeType = (String)getMimeByExtension(filesExtension.toLowerCase());
if (mimeType != null) {
response.setContentType(mimeType + "; name=\"" + fileToDownloadsName + "\"");
}
} catch (Exception e) {
getServletContext().log("Download had problems figuring out a MIME type. " + e);
return;
}
}
}
FileInputStream in = new FileInputStream(fileToDownload);
ServletOutputStream out = response.getOutputStream();
int bytesRead = 0;
byte byteArray[] = new byte[4096];
// Read in bytes through file stream, and write out through servlet stream
while( ( bytesRead = in.read( byteArray ) ) != -1 ) {
out.write( byteArray, 0, bytesRead );
}
in.close();
out.flush();
out.close();