使用Spring MVC返回PDF文件 [英] Return a PDF file with Spring MVC

查看:901
本文介绍了使用Spring MVC返回PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我具有该功能,我在其中设置了URL(ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...)的框架,然后该框架呈现了PDF文件.

Actually, I have that functionality, I got a frame where I set the URL (ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...) and that frame renders the PDF file.

但是我希望该URL隐藏...

But I want that URL hidden...

我需要使用Spring MVC返回一个PDF文件,但是该PDF是由另一个应用程序生成的.

I need return a PDF file with Spring MVC but that PDF is generated by another application.

这意味着我得到了另一个应用程序(Eclipse Birt Engine),该应用程序通过URL(ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...)传递参数,并且生成了PDF文件,我需要从控制器中获取该PDF并通过Spring MVC返回.有人可以帮忙吗?

This means I got another app (Eclipse Birt Engine) that I pass the parameters through of an URL (ip:port/birt/preview?__report=report.rptdesign&__format=pdf&parameters...) and it generates a PDF file, I need from my controller get that PDF and return it with Spring MVC. Could somebody help?

推荐答案

如下所示:

@Controller
@RequestMapping("/generateReport.do")
public class ReportController

    @RequestMapping(method = RequestMethod.POST)
    public void generateReport(HttpServletResponse response) throws Exception {

        byte[] data = //read PDF as byte stream

        streamReport(response, data, "my_report.pdf"));
    }

    protected void streamReport(HttpServletResponse response, byte[] data, String name)
            throws IOException {

        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment; filename=" + name);
        response.setContentLength(data.length);

        response.getOutputStream().write(data);
        response.getOutputStream().flush();
    }
}

这篇关于使用Spring MVC返回PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆