如何使用带有spring Framework的iframe在网页中显示pdf文件?

时间:2012-10-08 15:43:52

标签: spring-mvc

我想使用带有spring框架的iframe在网页中显示pdf文件。而不是下载选项,我必须在我的网页上显示pdf文件。请任何人都可以帮助我。

提前致谢..

我的java代码:

Employee employee = employeeManager.getObjectOrNull(
            Employee .class, id);
    File file = new File(dir,
            employee .getFileName());
    org.apache.commons.io.FileUtils.writeByteArrayToFile(file,
            employee .getfile());

    if (employee != null) {
        ControllerUtils.openFile(dir,
                response, employee.getFileName());
        String contentType = Utils.getContentType(employee 
                .getFileName());
        response.setContentType(contentType);
        response.setHeader("Cache-Control", "private, max-age=5");

1 个答案:

答案 0 :(得分:2)

我有解决方案。我使用了embed标记。我的HTML代码:

<embed width="900px" height="500px" name="plugin" src="/content/downloadPdf" type="application/pdf">

我的java代码

        response.setContentType("application/pdf");
        response.setHeader("Cache-Control", "private, max-age=5");

        response.setHeader("Pragma", "");
        if (file.length>0) {
            response.setContentLength(file.length);
        }
        ServletOutputStream ouputStream = response.getOutputStream();
        InputStream is = new ByteArrayInputStream(file);
        try {
            int b;
            while ((b = is.read()) != -1) {
                ouputStream.write(b);
            }
        } finally {
            ouputStream.flush();
            ouputStream.close();
            is.close();
        }