ServletOutputStream显示“打印”对话框

时间:2014-01-08 04:27:34

标签: java servlets printing dialog outputstream

是否可以为ServletOutputStream显示如下图像的打印框?

print box

我有一个javascript,它会打开新窗口,然后打开Servlet。如下面的代码

window.open("${pageContext.request.contextPath}/PrintingPDF?id="+data)

在servlet PrintingPDF中,我将contenttype设置为application / pdf。所以一旦在新窗口中加载pdf,我想显示打印对话框。下面是我在servlet PrintingPDF中的代码。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream out = null;
        FileInputStream in = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {

            String id = request.getParameter("id");         
            String downloadPath = ConfigUtil.getParameter("downloadpath");

            in = new FileInputStream(downloadPath + "/"+ id +".pdf");
            if(in!=null){
                response.setContentType("application/pdf");
                out = response.getOutputStream();
                bis = new BufferedInputStream(in);
                bos = new BufferedOutputStream(out);

                int ch = 0;
                while((ch=bis.read())!=-1){
                    bos.write(ch);
                }
            }

        }catch(DocumentException docEx){
            log.debug("document exception :: " + docEx.getMessage());
        }catch(Exception ex){
            log.debug("Exception ex :: " + ex.getMessage());
        }finally {
            bis.close();
            bos.close();
            in.close();
            out.close();
        }
    }

希望有人可以帮助我。

0 个答案:

没有答案