【问题标题】:retrieve p7m file from db2 blob field从 db2 blob 字段中检索 p7m 文件
【发布时间】:2012-11-20 18:26:38
【问题描述】:

我需要检索存储在 blob 字段中的 p7m pdf 文件,并通过指向特定的 url 将其直接下载。

现在我使用以下代码检索和发布 pdf,但打开使用查看数字签名工具保存的文件时,签名无效并且 pdf 似乎已损坏。

public byte[] getPdfOrdini(String xxx, String tipo) throws Exception, SQLException {
    Blob pdf;
    byte[] pdfData = null;
    Connection conn = new test().getConnectionOrdini();
    PreparedStatement pstmt = null;

    // Query
    if(tipo.equalsIgnoreCase("pnf"))
        pstmt = conn.prepareStatement("Select ... From .. Where ..");
    if(tipo.equalsIgnoreCase("pf"))
        pstmt = conn.prepareStatement("Select ... From .. Where .. = ?");
    pstmt.setString(1, xxx);
    ResultSet rset = pstmt.executeQuery();

    if(tipo.equalsIgnoreCase("pnf")){

        while (rset.next()) {
            pdf = rset.getBlob(1);
            pdfData = pdf.getBytes(1, (int) pdf.length());
        }

        //System.out.println("dimensione blob --> " + pdfData.length);
    }else{
        while (rset.next()) {
            pdf = rset.getBlob(1);
            pdfData = pdf.getBytes(1, (int) pdf.length());
        }
    }
    rset.close();
    pstmt.close();

    return pdfData;
}

以及用于发布 pdf 以供下载的代码:

<jsp:useBean id="PDF" class="pdf.test" scope="session" />
<%
Connection conn = null;

if ( request.getParameter("protocollo") != null )
{

String protocollo = request.getParameter("xxx") ;
String tipo = request.getParameter("type");   

try
{  
   String downloadFileName = "O" + xxx + ".pdf.p7m";
   conn = new pdf.test().getConnection();
   conn.setAutoCommit (false);  

   // get the image from the database
   byte[] pdfData = PDF.getPdfOrdini(xxx, tipo);   
   // display the image

   File pdf = new File(downloadFileName);
   FileOutputStream fos = new FileOutputStream(pdf);
   fos.write(pdfData);
   fos.flush();
   fos.close();

   response.setContentType( "application/x-download" );
   response.setHeader( "Content-Disposition", "attachment; filename=" + downloadFileName );
   conn.close();
}
catch (IllegalStateException il){
}
catch (Exception e)
{
  e.printStackTrace();
  throw e;
}
finally
{

}  
}
%>

感谢任何帮助。

【问题讨论】:

    标签: java pdf servlets


    【解决方案1】:

    您的两个用于阅读的 while 块是相同的,但无论如何它应该可以工作。
    你可以试试这个,在 DB2 上一直为我工作:

    byte[] pdfData = null;
    ResultSet rset = pstmt.executeQuery();
    if (rset.next())
    {
      pdfData = rset.getBytes(1);
    }
    conn.close();
    return pdfData;
    

    添加:
    接下来的事情是,我没有看到您在 JSP 中将数据写入浏览器。
    像这样的:

    byte[] pdfData = PDF.getPdfOrdini(xxx, tipo);
    char[] data = new char[pdfData.length];
    for (int i = 0; i < pdfData.length; i++) {
        data[i] = (char) pdfData[i];
        }
    response.setContentType( "application/x-download" );
    response.setHeader( "Content-Disposition", "attachment; filename=" + downloadFileName );
    response.setHeader("Content-length", Integer.toString(data.length));
    out.write( data, 0, data.length);
    out.flush();
    

    【讨论】:

    • 感谢您的建议,但我仍然得到相同的结果。我认为这是jsp中文件生成的问题,但我不明白问题出在哪里
    • 您确定数据正确存储在数据库中吗?
    • 是的,在我的 web 应用程序中,我还提供了一个 jsp,用于通过浏览器以可视化模式显示 pdf 文件,并且 pdf 已正确打开。问题是生成一个可下载的文件以检查数字签名信息。
    • 查看我编辑的答案:您是否将数据写入浏览器?
    • 对不起,out 对象的 write 方法需要 char 数组作为第一个参数,但我有字节数组。
    猜你喜欢
    • 2011-12-17
    • 2010-10-15
    • 2012-02-04
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多