Example of Downloading file from the server using JSP
JSP downloading file from server in jsp with examples of session tracking, implicit objects, el, jstl, mvc, custom tags, file upload, file download, interview questions etc.

Example of Downloading file from the server using JSP
In this example, we are going to download the jsp file. But you may download any file. For downloading the file from the server, you should specify the content type named APPLICATION/OCTET-STREAM.
index.jsp
This file provides a link to download the jsp file.
- <a href="download.jsp">download the jsp file</a>
download.jsp
In this example, we are downloading the file home.jsp which is located in the e: drive. You may 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();
- %>