【问题标题】:spring-mvc (portlet): how to return a pdf file in open file dialog?spring-mvc(portlet):如何在打开文件对话框中返回 pdf 文件?
【发布时间】:2011-01-04 10:43:39
【问题描述】:

在我的@ActionMapping 中,我为用户创建了一个 PDF 文件。 现在我想知道如何以保存/打开文件对话框的形式将此 pdf 返回给用户? 如果生成成功,我更喜欢显示下载链接。

我将 spring-mvc 3.0.5 与 portlet 结合使用。但是,如果有人对普通应用程序有一些指示,那么我可能会从那里弄清楚。 对于 2.0,我阅读了一些关于扩展 pdfgenerator 类和在 web.xml 中旋转的内容,但现在我们只需要 POJO 的......


编辑: Adeel 建议后的代码:

File file = new File("C:\\test.pdf");
        response.setContentType("application/pdf");

        try {
            byte[] b = new byte[(int) file.length()];
            OutputStream out = response.getPortletOutputStream();
            out.write(new FileInputStream(file).read(b));
            out.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "users/main";

【问题讨论】:

    标签: java spring spring-mvc portlet spring-portlet-mvc


    【解决方案1】:

    您可以将该文件直接写入您的response writer,并且不要忘记更改contentType。例如,

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment;filename=something.pdf");
    OutputStream out = response.getOutputStream();
    out.write(pdfFileContentInBytes);
    out.flush();                   
    

    好吧,我认为它是 HttpServletResponse 你所拥有的,但事实并非如此。当您使用 Portlet 时,它是一个 RenderResponse 对象。在互联网上搜索后,我发现一些链接可能对您有所帮助。

    • 首先以Lotus Form Server Portlet 为例,它展示了如何在使用portlet.xml 配置portlet 时允许多种mime-type。

    • 这里是Spring Portlet docs,它展示了我们如何使用portlet.xml 配置portlet。它有一个关于 mime-type 的 XML 元素,看看你能不能给出值,application/pdf,那里。

    另一个想法是将参数更改为ActionResponse response,而不是RenderResponse response。我在这里有点模糊,不确定您的超级课程是什么?它是什么方法?等等……

    对我来说,问题似乎是 portlet 响应的允许/不允许 mime 类型。

    【讨论】:

    • 添加到 Adeel 的答案中 ..content 类型应该是 application/pdf
    • @Mahesh:我就是这么做的 :)。还是谢谢。
    • 刚刚尝试过,我得到:应用程序/pdf 不是受支持的 mime/类型。但这似乎是正确的方向。
    • @Jack:令人惊讶!它是一个有效的 mime 类型。看看这个,tools.ietf.org/html/rfc3778。但是,尝试使用application/octet-stream 这应该可以。但我仍然建议解决为什么前者会给你错误。如果您分享实际代码和错误消息,我们也许可以说些什么。
    • 添加了一些代码。我正在使用 spring-portlet-mvc,所以我无法直接访问 HttpServletResponse(setHeader 需要,因为 RenderResponse 缺少该功能(可能是出于某种原因)。这是实际错误。application/pdf 不是支持的 mime 类型。
    【解决方案2】:

    在spring mvc中,ResourceResponse响应

    response.reset();
    response.setContentType("application/pdf");
    response.setProperty("Content-disposition", "attachment; filename=\"" +"example.pdf" +"\"");
    
    InputStream fontInputStream = request.getPortletSession()
                    .getPortletContext()
                    .getResourceAsStream("/WEB-INF/classes/arial.ttf");
    Document document = new Document(PageSize.A4, 40, 40, 40, 50);
    PdfWriter writer = PdfWriter.getInstance(document,
    response.getPortletOutputStream());
    document.addAuthor("XYZ");
    document.addTitle("ASDF");
    document.open();
    

    【讨论】:

    • setProperty() 作为 portlet 中 setHeader() 的替代品 - 很高兴知道这会起作用
    【解决方案3】:

    经过一段时间的研究,以下是答案: Serve PDF in Spring Portlet MVC Architecture - Liferay 6.0.6

    解决方案是使用 JSR 286 中的资源服务机制。ResourceResponse res 具有 res.setContentType("application/pdf"); 方法,因此您可以使用它来服务任何类型的资源。如果您需要将其作为附件下载,请使用:

    res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");

    【讨论】:

      【解决方案4】:

      我的代码:

      ResourceMapping("getPDF")

      public void descargarRecibo(ResourceRequest request,
              ResourceResponse response, PortletSession session,
              ModelMap modelMap) {
          FileInputStream fileInputStream = null;
          BufferedInputStream bufferedInputStream = null;
      
          String fileURL = "c:/intranetdoc/PDetalleLlamadas/file.pdf";
      
          try {
              fileInputStream = new java.io.FileInputStream(fileURL);
              OutputStream outputStream = response.getPortletOutputStream();
              response.setContentType("application/pdf");
              response.addProperty("Content-Disposition", "attachment; filename="
                      + fileName);
              bufferedInputStream = new java.io.BufferedInputStream(
                      fileInputStream);
              byte[] bytes = new byte[bufferedInputStream.available()];
              response.setContentLength(bytes.length);
              int aByte = 0;
              while ((aByte = bufferedInputStream.read()) != -1) {
                  outputStream.write(aByte);
              }
              outputStream.flush();
              bufferedInputStream.close();
              response.flushBuffer();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

      • 大声笑 - 抱歉 - 以为这是个问题 - 不过介绍几句就好了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      相关资源
      最近更新 更多