gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:58:40 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com