We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


Posted By

indianocean on 04/24/08


Tagged

java servlet


Versions (?)


Download file using a servlet


Published in: Java 


  1. FileInputStream fileToDownload = new FileInputStream("c:/file.xls");
  2. ServletOutputStream output = response.getOutputStream();
  3. response.setContentType("application/msexcel");
  4. response.setHeader("Content-Disposition", "attachment; filename=CombinedReportAdmin.xls");
  5. response.setContentLength(fileToDownload.available());
  6. int c;
  7. while ((c = fileToDownload.read()) != -1)
  8. {
  9. output.write(c);
  10. }
  11. output.flush();
  12. output.close();
  13. fileToDownload.close();

Report this snippet 

You need to login to post a comment.