pdf预览,通过后台返回文件流方式

本文介绍了一种在Web应用中实现PDF文件在线预览的方法,通过使用Java Servlet读取PDF文件并将其作为响应发送到浏览器,使得用户无需下载即可直接查看PDF内容。此方案涉及嵌入式HTML元素和服务器端代码,适用于需要提供文档预览功能的网站。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<div id="_div_embed" class="x-hidden" align="center" style="vertical-align: middle;">
        <embed id="_embed" style="vertical-align: middle;margin:10px;"type="application/pdf">
</div>
<script>
document.getElementById('_embed').src = 'showPdf.jhtml?path='+record.data.pdfpath;
</script>

controller:

@RequestMapping("/showPdf")
    public void showPdf(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        response.setContentType("application/pdf");
        FileInputStream in;
        OutputStream out;
        try {
            in = new FileInputStream(new File(request.getParameter("path")));
            out = response.getOutputStream();
            byte[] b = new byte[512];
            while ((in.read(b)) != -1) {
                out.write(b);
            }
            out.flush();
            in.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值