Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:00:43+00:00 2026-06-15T12:00:43+00:00

I need to retrieve a p7m pdf file stored in blob field and put

  • 0

I need to retrieve a p7m pdf file stored in blob field and put it for direct download by pointing a specific url.

Now I’m using the following code for retrieve and publish pdf, but opening the file saved with a tool for view digital signature the signature is not valid and the pdf seems like corrupted.

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;
}

and this code for publish pdf for download:

<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
{

}  
}
%>

any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:00:44+00:00Added an answer on June 15, 2026 at 12:00 pm

    Your two while blocks for reading are the same, but it should work anyhow.
    You may try this, worked always for me on DB2:

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

    Added:
    Next Thing is, I do not see you writing the data to the browser in your JSP.
    Something like this:

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to retrieve a value from the AndroidManifest.xml file, stored as a meta
i need to retrieve the page title from an URL i already have using
I need to retrieve the name of the stored procedure that a crystal report
I need to retrieve data from within a specific date range.Anybody can help me
I need to retrieve data from a specific time period. The query works fine
I need to retrieve values from CakePHP's config file database.php from one of my
I need to retrieve from an Application Server (JBoss) a large file (gigabytes) and
I need to retrieve the element with specific class which is placed below in
i need to retrieve all the city names from a specific country using openstreet
I need to retrieve product info. from database and now want to edit some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.