【问题标题】:Opening PDF file using window.open()使用 window.open() 打开 PDF 文件
【发布时间】:2013-08-10 11:16:47
【问题描述】:

我有一种方法可以生成 PDF 文件并将其存储在 /temp 文件夹中。我正在使用tomcat。我想打开 PDF 文件。我在 JavaScript 中使用 window.open() 方法尝试过这个。但是在点击超链接时,它说找不到请求的资源。

我使用以下行将 PDF 文件存储在 /temp 文件夹中(当然文件会生成并保存在该位置):

inputMap.put(TableProperties.PDF_PATH, "/temp/report.pdf");

现在在 JSP 中我尝试以下操作:

<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页面,打开文件的下载链接:

<tr>
  <td>
    <a href='<s:url action='gotoDownloadPdf'> </s:url>'>
      download
    </a>
  </td>
</tr>

struts.xml:

<action name="gotoDownloadPdf" class="com.stp.portal.view.SearchServicePortlet" method="gotoDownloadPdf">
            <result name="success">/WEB-INF/view/pdfDownload.jsp</result>   
        </action>

包含用于下载 PDF 文件的 JavaScript 的 JSP 页面:

<%@ 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 页面,但没有出现下载的弹出窗口。

【问题讨论】:

  • 请在句首加一个大写字母。还要为单词 I 使用大写字母,以及 JEE 或 WAR 等缩写词和首字母缩略词。这使人们更容易理解和帮助。

标签: java jsp tomcat struts2 liferay


【解决方案1】:

想想你在做什么。你真的认为在浏览器地址栏中输入http://www.google.com/tmp/secretReport.pdf 可以让你访问留在谷歌服务器文件系统上/tmp 目录中的一些秘密报告吗?

您不能像这样通过 HTTP 访问服务器的文件系统。您需要的是一个由浏览器调用的 servlet,它读取 PDF 文件并将其发送到 HTTP 响应中。

【讨论】:

  • 谢谢。你能给我举个例子吗?我用谷歌搜索了很多,但找不到任何可行的例子。
  • 不要使用 JSP 将文件发送到输出流。直接使用您的 Struts 操作。
【解决方案2】:

你错了:

  1. 该文件是在服务器上创建的,因此无法在客户端打开 使用 JavaScript
  2. 文件需要先下载,你需要提供映射到动作的url,可能带有你要下载哪个文件的参数。如果您在另一个操作中创建文件,您可以在 JSP 中显示要下载的文件的名称。只需使用 action 属性作为文件名。
  3. 不鼓励使用 scriptlet 中的 Java 代码,使用此代码实现用于返回 stream 结果的操作。

为了你的幸福,有一个example to download a file with struts2。您所需要做的就是将其应用到您的场景中。

【讨论】:

  • 谢谢,我试过了,但它给了我以下错误:~application/octet-stream is not a supported mime type~ 是因为我使用的是liferay吗?试了这么多天还是没解决……
  • 很高兴知道您正在使用liferay,因为它与portlet 相关而不是struts2,如果您不使用portlet 插件,那么您应该这样做,并在结果配置中尝试portlet 特定参数。 mime 时间是有效的,但它不是错误的原因。
【解决方案3】:

我看到其他人已经回答了你的结果,但我看到你很难理解他们的答案。好吧,那么我有一些更简单的开始。

只需在您的网页(站点根目录)目录中创建一个名为 /temp 的文件夹,然后放入 PDF (report.pdf) 即可使用该链接。

现在您只需使用简单的 File IO 将报告放入该文件中即可。

例如如果您编写代码以在temp 目录中写入名为xyz.pdf 的PDF 文件(已在您的Web 根目录中可用),那么访问URL 将为/temp/xyz.pdf

这里有一个简短的快速example on how to download file using struts2

【讨论】:

  • 得到以下错误:application/octet-stream is not a supported mime type 我正在使用 Liferay。是因为这个错误,
  • @user2670291:查看我对这个问题的回答stackoverflow.com/q/17721094/1700321
  • @Aleksandr:感谢您的回复,好像那个用户和我们遇到了同样的问题。我尝试将portletUrlType="resource" 用于&lt;s:url&gt; 标签。但同样的错误即将到来。下面是点击下载链接后生成的url:http://localhost:8080/web/guest/print-month-end-report?p_p_id=searchserviceportlet_WAR_SSDG_DEMOportlet&amp;p_p_lifecycle=0&amp;p_p_state=normal&amp;p_p_mode=view&amp;p_p_col_id=column-2&amp;p_p_col_count=1&amp;_searchserviceportlet_WAR_SSDG_DEMOportlet_struts.portlet.action=%2Fview%2Fview%2Fdownload&amp;_searchserviceportlet_WAR_SSDG_DEMOportlet_struts.portlet.mode=view
  • @user2670291:这个参数p_p_lifecycle=0表示你没有把portletUrlType="resource"放到url标签或者你没有正确部署你的war。
  • 我在jsp页面中写了以下内容:&lt;a href='&lt;s:url action='download' portletUrlType="resource"&gt; &lt;/s:url&gt;'&gt; download &lt;/a&gt;之后我清理了项目,我也清理了浏览器的缓存,但结果还是一样
猜你喜欢
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2015-01-09
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
相关资源
最近更新 更多