1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static void downloadFile(File file, String fileContentType, PageContext context) throws ApplicationException{
try{
HttpServletResponse response = (HttpServletResponse)context.getResponse();
// open download dialog box
if (file!=null){
if (!file.exists()) {
logger.error("File not found: "+file.getPath());
throw new ApplicationException("fileNotFound");
}
response.reset();
if (fileContentType!=null) response.setContentType(fileContentType);
response.setHeader("Content-Disposition", "attachment; filename="+ file.getName());
OutputStream outFile = response.getOutputStream();
InputStream inFile = new FileInputStream(file);
if (inFile == null) {
outFile.close();
}
else {
byte[] buffer = new byte[4096];
int len;
while ((len = inFile.read(buffer)) != -1) {
outFile.write(buffer, 0, len);
}
outFile.flush();
inFile.close();
outFile.close();
}
}
}
catch (ApplicationException e) {
throw e;
}
catch (Exception e) {
logger.error("Unable to download file: "+file.getName(),e);
}
} |
Partager