How to create a Force Download link for pdf file in JSP
Create 2 Files
DownloadPage.JSP code :-
downloadPdf.JSP code :-
DownloadPage.JSP code :-
<a href='downloadPdf.jsp?file=FILE.pdf' >Download PDF File</a>
downloadPdf.JSP code :-
<%@ page import="java.util.*,java.io.*"%>
<%
File f = new File ("E:/PDFfiles/Downloads/" + request.getParameter("file") );
response.setContentType ("application/pdf");
response.setHeader ("Content-Disposition", "attachment; filename="+request.getParameter("file"));
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>
![[Image]](http://1.bp.blogspot.com/_JUJEXEamsyU/SshOc9WnzqI/AAAAAAAAAnU/UzeP8mO7vLg/s320/twitter.png)
![[Image]](http://1.bp.blogspot.com/_JUJEXEamsyU/SshOe7yDQ4I/AAAAAAAAAnc/jJIkHcUIOMU/s320/stumbleupon.png)
![[Image]](http://1.bp.blogspot.com/_JUJEXEamsyU/SshOZ9bn8GI/AAAAAAAAAnE/gjmDwGLvQjg/s320/facebook.png)
![[Image]](http://2.bp.blogspot.com/_JUJEXEamsyU/SshObYfiTJI/AAAAAAAAAnM/_Te7NfXMY10/s320/rss.png)
![[Image]](http://3.bp.blogspot.com/_JUJEXEamsyU/SshOjAesZOI/AAAAAAAAAns/32PjuntiQ40/s320/technorati.png)

2 comments:
Feel free to leave comment if you like above widget, have any questions or just say Hi! :)