I have a method which generates a PDF file and stores it in /temp
folder. I am using tomcat. I want to open the PDF file. I have tried this using window.open()
method in the JavaScript. But on clicking the hyperlink it says the requested resource was not found.
I store the PDF file in /temp
folder using following line (of course the file gets generated and saved in the location):
inputMap.put(TableProperties.PDF_PATH, "/temp/report.pdf");
Now in a JSP I try the following:
<tr>
<td>
<a href="" onclick="javascipt:window.open('/temp
/report.pdf');" class="popup">Click to open.</a>
</td>
</tr>
but its showing the following error:
The requested resource was not found.
http://localhost:8080/temp/report.pdf
EDIT:
Tried the following:
in the JSP page, a download link to open the file:
<tr>
<td>
<a href='<s:url action='gotoDownloadPdf'> </s:url>'>
download
</a>
</td>
</tr>
in struts.xml
:
<action name="gotoDownloadPdf" class="com.stp.portal.view.SearchServicePortlet" method="gotoDownloadPdf">
<result name="success">/WEB-INF/view/pdfDownload.jsp</result>
</action>
a JSP page which contains the JavaScript to download the PDF file:
<%@ page import="java.util.*,java.io.*"%>
<%@ page language="java"%>
<!--Assumes that file name is in the request objects query Parameter -->
<%
//response.setHeader ("Cache-Control","no-cache");
//response.setHeader ("Pragma","no-cache");
//response.setHeader ("Expires",0);
//read the file name.
try
{
String fpath="/temp/";
String fileName="report.pdf";
fpath = fpath + fileName;
String fileType="pdf";
File f = new File (fpath);
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=\""+fileName+"\"");
//get the file name
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
//OPen an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c
InputStream inputStream = new FileInputStream(f);
ServletOutputStream servletOutputStream = response.getOutputStream();
int bit = 256;
int i = 0;
try
{
while ((bit) >= 0)
{
bit = inputStream.read();
servletOutputStream.write(bit);
}
//System.out.println("" +bit);
}
catch (Exception ioe)
{
//ioe.printStackTrace(System.out);
}
System.out.println( "\n" + i + " bytes sent.");
System.out.println( "\n" + f.length() + " bytes sent.");
servletOutputStream.flush();
//outs.close();
inputStream.close();
}
catch(Exception e)
{
}
%>
Now when I click on the download link, nothing happens, it redirects to the JSP page, but the pop up window for download does not appear.