Java 使用servlet在新窗口中打开pdf文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22602143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:46:05  来源:igfitidea点击:

open pdf file in new window using servlet

javahtmlservletspdfweb-applications

提问by user3150808

I am generating a pdf file for gate pass from my web application through servlet. I want to open this newly generated pdf in new window/tab and user should come back to the application from servlet. How can i open pdf in new window/tab? I am generating pdf from itext api. My servlet code snippet is:

我正在通过 servlet 从我的 Web 应用程序生成一个 pdf 文件,用于门通行证。我想在新窗口/选项卡中打开这个新生成的 pdf,用户应该从 servlet 返回到应用程序。如何在新窗口/选项卡中打开 pdf?我正在从 itext api 生成 pdf。我的 servlet 代码片段是:

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0,precheck=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close(); 

采纳答案by Jigar Joshi

In case if you are using GET request to make a servlet call

如果您使用 GET 请求进行 servlet 调用

GET set the target of link to target="_blank"

GET 将链接目标设置为 target="_blank"

<a href="/url/to/servlet" target="_blank"/>

POST

邮政

<form method="post" action="url/to/servlet"
  target="_blank">

so browser will make a new GET/POST request in new window/tab and then set the header Content-dispositionto inlineto render pdf inline instead of prompting a download window

因此浏览器将在新窗口/标签新GET / POST请求,然后将头Content-dispositioninline渲染PDF内嵌而非提示下载窗口

回答by NITIN RATHOUR

/*create jsp page*/   
<form action="OpenPdfDemo" method="post" target="_blank">
        <input type="submit" value="post">
    </form>
/* create servlet (servlet name = OpenPdfDemo)*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");



        ServletOutputStream  outs =  response.getOutputStream ();
//---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
response.setContentType( "application/pdf" );  // MIME type for pdf doc
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------

File file=new File("X://Books/struts book/sturts_Books/struts-tutorial.pdf");


//------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which  ignores this...
//------------------------------------------------------------
response.setHeader("Content-disposition","inline; filename=" +"Example.pdf" );

BufferedInputStream  bis = null; 
BufferedOutputStream bos = null;
try {

    InputStream isr=new FileInputStream(file);
    bis = new BufferedInputStream(isr);
    bos = new BufferedOutputStream(outs);
    byte[] buff = new byte[2048];
    int bytesRead;
    // Simple read/write loop.
    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
    }
} 
catch(Exception e)
{
    System.out.println("Exception ----- Message ---"+e);
} finally {
    if (bis != null)
        bis.close();
    if (bos != null)
        bos.close();
}


    }