/* set required headers */
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=DownloadFile.csv");
/* stream the file into a byte array */
byte[] downloadFile = buildDownloadFile();
/* write directly to the client */
OutputStream os = response.getOutputStream();
os.write(downloadFile, 0, downloadFile.length);
os.flush();
os.close();