User Tools

Site Tools


tips:java:sendfilejsp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tips:java:sendfilejsp [2009/06/08 12:12] (current)
erik created
Line 1: Line 1:
 +====== Send File to Browser using JSP ======
  
 +Taken from [[http://​www.fawcette.com/​javapro/​2002_03/​online/​online_eprods/​servlets_03_08/​|Send Files to Browser Clients the Right Way]]:
 +
 +<code java>
 +<%
 +  // fetch the file
 +  String filename = "​companySecret.txt";​
 +  String filepath = "​C:​\\";​
 +
 +  response.setContentType("​application/​octet-stream"​);​
 +  response.setHeader("​Content-Disposition",​
 +                     "​attachment;​ filename=\""​ + filename + '"'​);​
 +
 +  java.io.FileInputStream fileInputStream =
 +                     new java.io.FileInputStream(filepath + filename);
 +
 +  int i;
 +  while ((i=fileInputStream.read()) != -1) {
 +    out.write(i);​
 +  }
 +
 +  fileInputStream.close();​
 +  out.close();​
 +%>
 +</​code>​
tips/java/sendfilejsp.txt · Last modified: 2009/06/08 12:12 by erik