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
|
if (request.getParameter("filename") != null)
{
String filename = request.getParameter("filename") ;
response.setContentType("application/download");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
ServletOutputStream out = response.getOutputStream();
File file = null;
BufferedInputStream from = null;
try
{
file = new File(filename);
response.setContentLength((int) file.length());
int bufferSize = 64 * 1024;
long time = System.currentTimeMillis();
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) {}
}
time = (System.currentTimeMillis() - time) / 1000; // seconds download
double kb = (file.length() * 1.0 / 1024);
if (file != null)
file.delete();
} catch (Exception e) {
return;
} finally {
try { file.delete();} catch (Exception ex) {}
}
} |
Partager