0

当我执行这个jsp文件时,我可以下载pdf格式的文件。但是当我试图打开它时,它会显示一个不支持格式的错误。但是,如果我尝试在记事本中打开下载的 pdf 文件,我会得到我想要的数据。但我应该以解密的形式获得它。所以请帮我打开 pdf 文件以查看数据。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%  String ID = (String) session.getAttribute("ID");
    String connectionURL = "jdbc:oracle:thin:@localhost:1521:XE";
    String url = request.getParameter("WEB_URL");
    String Content = new String("");
    Statement stmt = null;
    Connection con = null;


String Content = new String("");
                String filename = "data" + ID + ".pdf";

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(connectionURL, "temp",

            "root");
            stmt = con.createStatement();
            String qry = "select * from form where id='" + ID + "'";
            ResultSet rst = stmt.executeQuery(qry);
            if (rst.next()) {
                Content = "  FirstName: " + rst.getString("FirstName")
                        + "\r\n" + "  LastName: "
                        + rst.getString("LastName") + "\r\n" + "  Age: "
                        + rst.getString("Age") + "\r\n" + "  City: "
                        + rst.getString("City") + "\r\n" + "  Phone: "
                        + rst.getString("Phone") + "\r\n" + "  ID: "
                        + rst.getString("ID");
            }
            //out.println(Content);
            byte requestBytes[] = Content.getBytes();
            response.reset();
            response.setContentType("application/pdf");
            response.setHeader("cache-control", "no-cache");
            response.setHeader("Content-disposition",
                    "attachment; filename=" + filename);
            response.setCharacterEncoding("UTF-8");

            ByteArrayInputStream byteStream = new ByteArrayInputStream(
                    requestBytes);
            BufferedInputStream bufStream = new BufferedInputStream(
                    byteStream);
            ServletOutputStream responseOutputStream = response
                    .getOutputStream();
            int data = bufStream.read();
            while (data != -1) {
                responseOutputStream.write(data);
                data = bufStream.read();
            }

            bufStream.close();
            responseOutputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(" Exception is:" + e);
        }
    %>
</body>
</html>
4

1 回答 1

3

主要是什么问题,您也在 PDF 输出周围输出 HTML。确保以没有最终换行符开始<%和结束;%>那是很好的风格,即使在 response.reset()这里可以发挥作用。

此外content.getBytes("Cp1252")(Windows Latin-1,ISO-8859-1 的扩展)。否则平台编码是默认的。

看来您输出的是字符串Content而不是 pdf(文件名)。

File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
FileInputStream byteStream = new FileInputStream(file);

由于 PDF 是二进制的,因此不需要 setCharacterEncoding。但是,对于文件,您可以设置 Content-Length 标头。

为了填写个性化数据,需要更多。


早期做:

        File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
        if (!file.canRead()) {
            // Log this with filename...
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        long fileLength = file.length();
        if (fileLength > Integer.MAX_VALUE) { // Too large
            // Log this with filename...
            response.sendError(HttpServletResponse.SC_CONFLICT);
            return;
        }
        int length = (int) fileLength;

后:

        response.setHeader("Content-disposition",
                "attachment; filename=" + filename);

        response.setContentLength(length);

        FileInputStream byteStream = new FileInputStream(file);
        BufferedInputStream bufStream = new BufferedInputStream(
                byteStream);
        ServletOutputStream responseOutputStream = response
                .getOutputStream();
        final int BLOCK_SIZE = Math.min(16*1024, length);
        byte[] data = new byte[BLOCK_SIZE];
        while (length > 0) {
            int nread = bufStream.read(data, 0, Math.max(length, block.length));
            if (nread <= 0) {
                // Something fishy happened, upload of other smaller version.
                break;
            }
            length -= nread;
            responseOutputStream.write(data, 0, nread);
        }
        bufStream.close();
于 2013-01-30T18:44:21.373 回答