logo

Example Of Downloading File From The Server Using JSP


Show

index.jsp

This file gives a link to download the jsp file.

<a href="download.jsp">download the jsp file</a>

download.jsp

In this example, we're downloading the file home.jsp that's positioned in the e: drive. You may also change this location accordingly.

<%    
  String filename = "home.jsp";   
  String filepath = "e:\\";   
  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();   
%>

download this example