在jsp中显示pdf [英] Displaying pdf in jsp

查看:188
本文介绍了在jsp中显示pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个jsp页面来显示pdf的内容,但最终在jsp中使用了ascii代码。我想在jsp中显示pdf的内容。什么是我错过的部分。当我尝试用pdf编写阅读内容时,它只显示ascii值而不是可读格式

I have written a jsp page to display contents of pdf, but end up with ascii codes in jsp. I want to display the contents of pdf in jsp. Whats the part that I have missed. When I try to write the read content in pdf it shows only ascii values and not in readable format

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=${encoding}"></head>

<%@page import="java.io.File"%>
<%@page import="java.io.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="com.itextpdf.text.Image"%>
<%@page import="com.itextpdf.text.Document"%>
<%@page import="com.itextpdf.text.DocumentException"%>
<%@page import="com.itextpdf.text.pdf.PdfReader"%>
<%@page import="com.itextpdf.text.pdf.PdfImportedPage"%>
<%@page import="com.itextpdf.text.pdf.PdfWriter"%>
<%@page import="com.itextpdf.text.pdf.PdfContentByte"%>
<%@ page language="java" contentType="application/pdf; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
    response.reset();
    response.setContentType("application/pdf");
    File file = new File("D:\\TNWRD_Documents\\CHAPTER_II.pdf");
    response.setHeader("Content-Type", "application/pdf");
    response.setHeader("Content-Disposition",
            "inline;filename=Saba_PhBill.pdf");
    response.setContentLength((int) file.length());
    response.setHeader("Content-Type",
            getServletContext().getMimeType(file.getName()));
    response.setHeader("Content-Length", String.valueOf(file.length()));
    //OPen an input stream to the file and post the file contents thru the
    //servlet output stream to the browser
    FileInputStream in = new FileInputStream(file);
    ServletOutputStream outs = response.getOutputStream();
    response.setContentLength(in.available());
    byte[] buf = new byte[8192];
    int c = 0;
    try {
        while ((c = in.read(buf, 0, buf.length)) > 0) {
            //System.out.println("size:"+c);
            outs.write(buf, 0, c);
            out.write(outs.toString());
        }

    } catch (IOException ioe) {
        ioe.printStackTrace(System.out);
    } finally {
        outs.flush();
        outs.close();
        in.close();
    }
%>
</html>


推荐答案

JSP是服务于工作的错误工具文件下载。 JSP被设计为一种视图技术,旨在使用taglib和EL轻松生成HTML输出。基本上,使用您的JSP方法,您的PDF文件会混乱<!DOCTYPE> < html> 等标签因此已损坏且无法识别为有效的PDF文件。这是使用<的原因之一em> scriptlets 是一种不好的做法。它让你完全混淆了应该如何工作。在这种特殊情况下,使用普通的Java类进行文件下载工作。

JSP is the wrong tool for the job of serving a file download. JSP is designed as a view technology with the intent to easily produce HTML output with taglibs and EL. Basically, with your JSP approach, your PDF file is cluttered with <!DOCTYPE>, <html> etc tags and therefore corrupted and not recognizable as a valid PDF file. This is by the way one of the reasons why using scriptlets is a bad practice. It has namely completely confused you as to how stuff is supposed to work. In this particular case, that is using a normal Java class for the file download job.

你应该使用 servlet 。这是一个启动示例,假设Servlet 3.0和Java 7可用:

You should be using a servlet instead. Here's a kickoff example, assuming that Servlet 3.0 and Java 7 is available:

@WebServlet("/foo.pdf")
public class PdfServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File file = new File("/absolute/path/to/foo.pdf");
        response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"foo.pdf\"");
        Files.copy(file.toPath(), response.getOutputStream());
    }

}

(如果是Servlet 3.0)不可用,然后将其映射到 web.xml 通常的方式,如果Java 7不可用,则使用读/写循环通常的方式

(if Servlet 3.0 is not available, then map it in web.xml the usual way, if Java 7 is not available, then use a read/write loop the usual way)

只需将此类完整地复制到您的项目中,然后按打开所需的PDF文件/contextpath/Saba_PhBill.pdf 而不是 /contextpath/youroriginal.jsp (在一个包中组织它并在类中自动完成必要的导入之后) , 当然)。

Just copypaste this class in its entirety into your project and open the desired PDF file by /contextpath/Saba_PhBill.pdf instead of /contextpath/youroriginal.jsp (after having organized it in a package and autocompleted the necessary imports in the class, of course).

例如。如下所示,在JSP中您要显示PDF内联:

E.g. as follows in a JSP where you'd like to show the PDF inline:

<object data="${pageContext.request.contextPath}/Saba_PhBill.pdf" type="application/pdf" width="500" height="300">
    <a href="${pageContext.request.contextPath}/Saba_PhBill.pdf">Download file.pdf</a>
</object>

< a> application / pdf 内容时,即当它没有安装Adobe Reader插件时,>链接意味着优雅降级)

(the <a> link is meant as graceful degradation when the browser being used doesn't support inlining application/pdf content in a HTML document, i.e. when it doesn't have Adobe Reader plugin installed)

  • Simplest way to serve static data from outside the application server in a Java web application
  • Abstract template for a static resource servlet supporting ETags, caching, etc

这篇关于在jsp中显示pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆