使用window.open()打开PDF文件 [英] Opening PDF file using window.open()
问题描述
我有一种生成PDF文件并将其存储在/temp
文件夹中的方法.我正在使用tomcat.我想打开PDF文件.我已经在JavaScript中使用 window.open()
方法尝试了此操作.但是,在单击超链接时,它说未找到所请求的资源.
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.
我使用以下行将PDF文件存储在/temp
文件夹中(当然,该文件会生成并保存在该位置):
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");
现在在JSP中,我尝试以下操作:
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>
,但显示以下错误:
The requested resource was not found.
http://localhost:8080/temp/report.pdf
尝试了以下内容:
在JSP页面中,有一个下载链接来打开文件:
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>
在 struts.xml
中:
in struts.xml
:
<action name="gotoDownloadPdf" class="com.stp.portal.view.SearchServicePortlet" method="gotoDownloadPdf">
<result name="success">/WEB-INF/view/pdfDownload.jsp</result>
</action>
一个JSP页面,其中包含用于下载PDF文件的JavaScript:
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)
{
}
%>
现在,当我单击下载链接时,什么也没有发生,它重定向到JSP页面,但是没有出现用于下载的弹出窗口.
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.
推荐答案
思考您在做什么.您是否真的认为在浏览器地址栏中输入 http://www.google.com/tmp/secretReport.pdf
可以让您访问/tmp
google服务器文件系统上的目录?
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?
您不能像这样通过HTTP访问服务器的文件系统.您需要的是一个由浏览器调用的servlet,该servlet读取PDF文件并将其发送到HTTP响应中.
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.
这篇关于使用window.open()打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!