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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
public void execute() throws IOException{
fres.setHeader("Content-Disposition", "attachment;filename=\"" + nameFileExport + "\"");
String nameFileExport = "file.csv";
fres.setCharacterEncoding("iso-8859-1");
fres.setContentType("text/plain");
BufferedWriter(getResponse().getWriter());
ServletOutputStream out = fres.getOutputStream();
File file = null;
BufferedInputStream from = null;
try {
//dans mon cas le filepath et le path complet après création
// d'un temp file
file = new File("c:\\TEMP\\"+nameFileExport);
fres.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();
}else{
System.out.println("file null");
}
} catch (Exception e) {
return;
} finally {
try {
file.delete();
} catch (Exception ex) {}
}
}
public void setServletResponse(HttpServletResponse res) {
fres = res;
} |
Partager