Skip to Content
author's profile photo Former Member
Former Member

JDBC: Send BLOB field to a file

Hi all,

I'm sending a JDBC record to a file but I'm wondering how I can send a BLOB (Binary large object) to a file.

Can anyone advise me how I can write such a file (eg a JPEG file) on a directory?

Thanks in advance!

Regards,

Wouter.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jun 10, 2008 at 12:35 PM
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 10, 2008 at 12:40 PM

    Hello,

    Yes it is possible to do using the Java mapping .You need to do XI Binary Conversion to process it.

    Use this link for Step by Step procedures.

    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/10dd67dd-a42b-2a10-2785-91c40ee56c0b

    hope this will solve your requirement.

    *************Rewarad points,if found useful

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi,

      I have found some java code that should help me write a BLOB column to a OS file:

          /**
           * Method used to write the contents (data) from an Oracle BLOB column to
           * an O/S file. This method uses one of two ways to get data from the BLOB
           * column - namely the getBytes() method. The other way to read data from an
           * Oracle BLOB column is to use Streams.
           * 
           * @throws java.io.IOException
           * @throws java.sql.SQLException
           */
         // public void readBLOBToFileGet()
           //       throws IOException, SQLException {
              Connection      conn                    = null;
              String          outputBinaryFileName1   = null;
              File            outputBinaryFile1       = null;
              FileOutputStream    outputFileOutputStream      = null;
              String              sqlText                     = null;
              Statement           stmt                        = null;
              ResultSet           rset                        = null;
              BLOB                image                       = null;
              long                blobLength;
              long                position;
              int                 chunkSize;
              byte[]              binaryBuffer;
              int                 bytesRead                   = 0;
              int                 bytesWritten                = 0;
              int                 totbytesRead                = 0;
              int                 totbytesWritten             = 0;
      
              try {
      
                  stmt = conn.createStatement();
      
                  outputBinaryFile1       = new File(outputBinaryFileName1);
                  outputFileOutputStream  = new FileOutputStream(outputBinaryFile1);
      
                  sqlText = 
                      "SELECT image " +
                      "FROM   test_blob " +
                      "WHERE  id = 1 " +
                      "FOR UPDATE";
                  rset = stmt.executeQuery(sqlText);
                  rset.next();
                  image = ((OracleResultSet) rset).getBLOB("image");
                  
                  blobLength = image.length();
                  chunkSize = image.getChunkSize();
                  binaryBuffer = new byte[chunkSize];
                  
                  for (position = 1; position <= blobLength; position += chunkSize) {
                      
                      // Loop through while reading a chunk of data from the BLOB
                      // column using the getBytes() method. This data will be stored
                      // in a temporary buffer that will be written to disk.
                      bytesRead = image.getBytes(position, chunkSize, binaryBuffer);
      
                      // Now write the buffer to disk.
                      outputFileOutputStream.write(binaryBuffer, 0, bytesRead);
                      
                      totbytesRead += bytesRead;
                      totbytesWritten += bytesRead;
      
                  }
      
                  outputFileOutputStream.close();
                  
                  conn.commit();
                  rset.close();
                  stmt.close();
                  
                  System.out.println(
                      "==========================================================\n" +
                      "  GET METHOD\n" +
                      "==========================================================\n" +
                      "Wrote BLOB column data to file " + outputBinaryFile1.getName() + ".\n" +
                      totbytesRead + " bytes read.\n" +
                      totbytesWritten + " bytes written.\n"
                  );
      
              } catch (IOException e) {
                  System.out.println("Caught I/O Exception: (Write BLOB value to file - Get Method).");
                  e.printStackTrace();
                  throw e;
              } catch (SQLException e) {
                  System.out.println("Caught SQL Exception: (Write BLOB value to file - Get Method).");
                  System.out.println("SQL:\n" + sqlText);
                  e.printStackTrace();
                  throw e;
              }
      
          //}
      

      But how I should import the package in order to use the BLOB datatype?

      Thx already for your help!

      Regards,

      Wouter.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.