spring-mvc(portlet):如何在打开文件对话框中返回pdf文件?

发布于 2024-10-09 22:57:50 字数 903 浏览 8 评论 0原文

在我的 @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";

In my @ActionMapping I create a PDF file for the user.
Now I was wondering how I can return this pdf to the user in the form of a save/open file dialog box?
I'd prefer this over showing a download link if the generation was succesful.

I'm using spring-mvc 3.0.5 in combination with portlets. But if anyone has some pointers for a normal application then I can probably figure it out from there.
For 2.0 I read something about extending a pdfgenerator class and twidling in the web.xml but since nowadays we just need POJO's....


Edit:
Code after Adeel's suggestion:

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

划一舟意中人 2024-10-16 22:57:50

您可以将该文件直接写入您的响应编写器,并且不要忘记更改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 类型。

  • 这里是 Spring Portlet 文档,它显示我们如何使用 portlet.xml 配置 portlet。它有一个关于 mime-type 的 XML 元素,看看你是否可以给出值,application/pdf,在那里。

另一个想法是将参数更改为 ActionResponse 响应,而不是 RenderResponse 响应。我在这里有点模糊,不确定你的超级类是什么?它是什么方法?等等......

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

You can write that file directly to your response writer, and don't forget to change the contentType. For example,

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

Well, I thought its a HttpServletResponse what you have, but its not the case. As you are working with Portlet, its a RenderResponse object. After searching over the Internet, I found few links which might be helpful to you in this regard.

  • First take an example of Lotus Form Server Portlet, its showing the way how to allow multiple mime-type while configuring the portlet using portlet.xml.

  • Here is Spring Portlet docs, its showing how we configure portlet using portlet.xml. It has an XML element about mime-type, see if you can give the value, application/pdf, there.

Another idea is to change your parameter to ActionResponse response, instead of RenderResponse response. I am a bit blur here, not sure what is your super class? what method is it? etc....

To me, it seems that the problem is allowed/not-allowed mime-types for the portlet response.

雨落□心尘 2024-10-16 22:57:50

在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();

in spring mvc, ResourceResponse response

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();
万人眼中万个我 2024-10-16 22:57:50

这是我研究了一段时间后的答案:
在 Spring Portlet MVC 架构中提供 PDF - Liferay 6.0 .6

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

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

Here's the answer after I worked it out for a little while:
Serve PDF in Spring Portlet MVC Architecture - Liferay 6.0.6

The solution is to use Resource Serving Mechanism from JSR 286. The ResourceResponse res has a res.setContentType("application/pdf"); method so you can use it to serve any type of resources. If you need it to be downloaded as an attachment, then use this:

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

吹泡泡o 2024-10-16 22:57:50

我的代码:

资源映射(“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();
    }
}

My code:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文