Typically you write the following code in the service method of a servlet to download a file as following:
FileInputStream fileToDownload = new FileInputStream(filePath);
ServletOutputStream writer = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename="+fileName);
int c;
while((c=fileToDownload.read()) != -1){
writer.write(c);
}
writer.flush();
writer.close();
fileToDownload.close();
This code might give some problems in internet explorer and the behavior is not consistent. May be different versions behave in different ways when user downloads a file. In one browser, in the ‘Download File’ dialog, if you click on save buttons, it saves file correctly and you can open the file, but you can not open the file directly from ‘Download File’ dialog. In other browser, even the save was not working properly.
If the internet explorer gives the following error,
“Internet explorer can not download ********** file from ****. Internet explorer was not able to open this internet site. The requested site is either unavailable or cannot be found. Please try again later”
Then try to add the following code after setting content-disposition,
response.setHeader("Cache-Control", "cache,must-revalidate");
response.addHeader("Pragma", "public");
These two lines should hopefully solve the above problem.