java - 从 db2 blob 字段检索 p7m 文件

标签 java pdf servlets

我需要检索存储在 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
{

}  
}
%>

感谢任何帮助。

最佳答案

您的两个 while 读取 block 是相同的,但无论如何它应该可以工作。
您可以尝试这个,它在 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();

关于java - 从 db2 blob 字段检索 p7m 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680704/

相关文章:

java - servlet 过滤器中的 URL 映射问题

java - Java 8's HashMap misbehaves if the keys implement Comparable in a way that isn' t 与equals一致是不是bug?

java - Spring MVC 中的过滤器映射不适用于某些 URL

java - 在Java中将字符串数组转换为字符串

java - PDF 中不保留双倍空格

pdf - Flutter创建具有不同页面大小和字体大小的pdf文件

Tomcat Servlet 默认日期时间格式

mysql - 在另一个java类文件中添加一个java类文件

java - 从 SQLiteDB 获取选择信息?

c++ - 在 Windows 8.1 上使用 MuPDF 创建 pdf 图像注释