繁体   English   中英

如何使用servlet在浏览器中编写pdf文件?

[英]how to write pdf file in browser using servlet?

这里下面的代码试图将pdf文件从本地机器写入浏览器,但这里没有将文件写入浏览器

String pdfFileName = "hello1.pdf";
            String contextPath = "";
                    contextPath = "/home/admin/Desktop/";
            File pdfFile = new File(contextPath + pdfFileName);
                    FileInputStream fileInputStream = new FileInputStream(pdfFile);
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
            response.setContentLength((int) pdfFile.length());      
            OutputStream responseOutputStream = response.getOutputStream();
                    System.out.println("fileInputstream length : " + fileInputStream.available());
            int length;
                    byte[] buffer = new byte[4096];
            while ((length = fileInputStream.read(buffer)) > 0) {
                responseOutputStream.write(buffer, 0, length);
            }
                    System.out.println(" outputstream length : " + responseOutputStream.toString());
                    fileInputStream.close();
                    responseOutputStream.flush();
                    responseOutputStream.close();

在你的代码声明中:

response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);

将“附件”更改为“内联”,如下所示:

response.addHeader("Content-Disposition", "inline; filename=" + pdfFileName);

然后pdf文件将自动打开

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM