How to Download File from Server using JSP

Let's start learning about the downloading the files from remote server by using Java server pages (JSP).
 
For doing this, we have to write two files one for giving the link of the file to be downloaded and other one of JSP in which we will code for downloading the file.
 
 index.jsp
 
This is the file where we give the link for that file which we are going to download.

<a href="download.jsp">Download JSP file</a>
 
The above is the HTML tag which provides a link to download the file. 
 
Now we have to make the JSP file which contains the coding of downloading the file and we will make that file as download.jsp and we will show only JSP coding apart from HTML coding. for downloading we should specify the content type named APPLICATION/OCTET-STREAM.
 
download.jsp 
 
In the below example we are going to show the demo coding of downloading admin.jsp file from remote server which is located in c:drive(let's say) at server, you can change the location as per your convenience.
 
<%
String filename="admin.jsp";
String filepath="c:\\";
respose.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filenmae=\""+filename+"\"");
java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath+filename);
int i;
while((i=fileInputStream.read())!=-1)
{
out.write(i);
}
fileInputStream.close();
%>
Ebook Download
View all
Learn
View all