问题
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
-----------EDITED--------------
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.
回答1:
Think about what you're doing. Do you really think that typing http://www.google.com/tmp/secretReport.pdf
in your browser address bar would give you access to some secret report left in the /tmp
directory on the file system of the google server?
You can't access the file system of a server over HTTP like this. What you need is a servlet, invoked by the browser, that reads the PDF file and sends it in the HTTP response.
回答2:
What you have wrong:
- The file is created on server, thus it can't be opened on the client using JavaScript
- The file needs to download first, you need to provide the url mapped to the action, probably with parameter which file you want to download. If you create a file in another action, you could show the name of the file in the JSP to download. Just use the action property for file name.
- Java code in scriptlets is discouraged, use this code to implement the action used to return a
stream
result.
To your happiness there's an example to download a file with struts2. All you need is to apply it to your scenario.
回答3:
I see other's have already answered your result, but I see you are having difficulty in understanding their answer. Well, then I have something simpler to start from.
Simply create a folder named /temp in your Web Pages (site root) directory and then put a PDF (report.pdf) then that link will work.
Now all you have to do is simply put your report in that file using simple File IO.
For e.g. if you wrote code to write a PDF file named xyz.pdf in the temp directory (already available in your web-root) then the access url will be /temp/xyz.pdf
Hope so this one helps.
Here's a small quick example on how to download file using struts2
来源:https://stackoverflow.com/questions/18161332/opening-pdf-file-using-window-open-from-server