I am trying to display a save as dialog box to save a PDF file. This should happen on click of a link in the JSP. On click of this link I call a struts action which prepares PDF and forwards to another JSP 'download.jsp' which I want to use for the download box.
I tried using 'document.execCommand' but it seems this only works for .txt and .html files.
Then I tried doing it by setting response headers and the content type. Following is the code in my download.jsp. But this writes junk characters to my browser.
Please let me know a solution for this problem.
<%@ page language="java" import="javax.servlet.ServletOutputStream,java.io.BufferedOutputStream,java.io.DataInputStr eam,java.io.File,java.io.FileInputStream"%><%
response.setHeader("Content-Disposition","attachment;filename=\"" + "mandateOut.pdf" + "\"");
response.setContentType( "application/pdf" );
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
File file = new File(<fully qualified file name>);
response.setContentLength( (int)file.length() );
// get the OutputStream and buffer our bytes to it
ServletOutputStream outStream = response.getOutputStream();
byte[] buf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(file));
int len;
while ((in != null) && ((len = in.read(buf)) != -1))
{
outStream.write(buf,0,len);
}
// keep the page from freaking out over our use of it's outputstream
in.close();
outStream.flush();
outStream.close();
%>