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
| public void sendResponse(String filename, HttpServletResponse resp, BaseChemin chemin) throws IOException
{
resp.setContentType("application/download");
resp.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
ServletOutputStream out = resp.getOutputStream();
File file = null;
BufferedInputStream from = null;
try {
file = new File(chemin.getNomRelatif());
resp.setContentLength((int) file.length());
int bufferSize = 64 * 1024;
try {
from = new BufferedInputStream(new FileInputStream(file), bufferSize * 2);
byte[] bufferFile = new byte[bufferSize];
for (int i = 0; ; i++) {
int len = from.read(bufferFile);
if (len < 0) break;
out.write(bufferFile, 0, len);
}
out.flush();
} finally {
try {
from.close();
} catch (Exception e) { }
try {
out.close();
} catch (Exception e) {}
}
if (file != null) file.delete();
} catch (Exception e) {
return;
} finally {
try {
file.delete();
} catch (Exception ex) {}
}
} |
Partager