0

我想使用带有 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");
4

1 回答 1

2

I've got the solution. I used embed tag. My HTML code:

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

My java code

        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();
        }
于 2012-10-09T04:30:06.360 回答