Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SOLVED...FOR NOW...Loss of quality reading image BLOB from database

492395Oct 19 2006 — edited Oct 23 2006
Hi,

I have written a small stub program on which I will build an image gallery web app. For now, this stub reads an image file from the local filesystem, and streams it into a BLOB column. It then streams this BLOB column back to the loca filesystem, with a different filename. This all works fine without any exceptions. However, the file reas from the BLOB column is smaller than the original file, and has a coloured banding effect at the bottom of the image, as if some data was missing. Here is my code:

//-------------------------------------------------------------------
// LoadImage.java
//-------------------------------------------------------------------

/*
*===================================================================
* Used to upload a local filesystem image file into a blob column
* of an Oracle database. The file may be scaled before upload.
*===================================================================
*/

// import standard classes
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.sql.*;
import java.util.*;

// JPEG codec class
import com.sun.image.codec.jpeg.*;

// Oracle extended JDBC classes.
import oracle.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.driver.OracleDriver;


public class LoadImage
{
public static String inputFileName = null;
public static String outputFileName = null;
public static String scaledOutputFileName = null;
public static File inputFile = null;
public static File outputFile = null;
public static File scaledOutputFile = null;
public static FileInputStream inputFileInputStream = null;
public static FileOutputStream outputFileOutputStream = null;
public static InputStream blobInputStream = null;
public static OutputStream blobOutputStream = null;
public static String sqlText = null;
public static Statement stmt = null;
public static ResultSet rs = null;
public static BLOB imageBlob = null;
public static int bufferSize;
public static byte[] byteBuffer;
public static byte[] binaryBuffer;
public static int chunkSize;
public static int bytesRead = 0;
public static int bytesWritten = 0;
public static int totalBytesRead = 0;
public static int totalBytesWritten = 0;
public static final String DB_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
public static final String DB_URL = "<URL>";
public static final String DB_USERNAME = "<SCHEMA>";
public static final String DB_PASSWORD = "<PASSWORD>";
public static Connection conn = null;
public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter screen = new PrintWriter(System.out, true);

public static void main(String[] args )throws SQLException,
ClassNotFoundException,
IOException
{
// establish DB connection
Class.forName(DB_DRIVER_CLASS);
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
// disable autocommit to prevent mid-stream commit
conn.setAutoCommit(false);
stmt = conn.createStatement();

// set up input/output files and streams
inputFileName = "ball_bearings.jpg";
inputFile = new File(inputFileName);
inputFileInputStream = new FileInputStream(inputFile);
outputFileName = "ball_bearings_db.jpg";
outputFile = new File(outputFileName);
outputFileOutputStream = new FileOutputStream(outputFile);


// insert record with empty BLOB locators
sqlText = "INSERT INTO images (user_id, name, description, album_id, is_approved, publish_date, full_image, thumb_image) " +
"VALUES ('u3346', 'Test Name', 'Test Description', 21, 0, SYSDATE, EMPTY_BLOB(), EMPTY_BLOB())";

stmt.executeUpdate(sqlText);

// retrieve BLOB column from last row
sqlText = "SELECT full_image " +
"FROM images " +
"WHERE rownum = 1 " +
"ORDER BY image_id DESC";

rs = stmt.executeQuery(sqlText);
rs.next();
// get Blob
imageBlob = ((OracleResultSet)rs).getBLOB("full_image");
// get buffer size required for BLOB
bufferSize = imageBlob.getBufferSize();
// byteBuffer for BLOB processing
byteBuffer = new byte[bufferSize];
// get an OutputStream for writing to the BLOB
blobOutputStream = imageBlob.getBinaryOutputStream();

// read contents of input file to bytesRead[]
while ((bytesRead = inputFileInputStream.read(byteBuffer)) != -1)
{
blobOutputStream.write(byteBuffer, 0, bytesRead);
totalBytesRead += bytesRead;
totalBytesWritten += bytesWritten;
}

// retrieve BLOB column from last row inserted
sqlText = "SELECT full_image " +
"FROM images " +
"WHERE rownum = 1 " +
"ORDER BY image_id DESC";

rs = stmt.executeQuery(sqlText);
rs.next();
// get BLOB
imageBlob = ((OracleResultSet)rs).getBLOB("full_image");
// get an InputStream for reading the BLOB
blobInputStream = imageBlob.getBinaryStream();
// get BLOB chunk size
chunkSize = imageBlob.getChunkSize();
//binaryBuffer for BLOB processing
binaryBuffer = new byte[chunkSize];

// reset byte counters
totalBytesRead = 0;
totalBytesWritten = 0;
bytesRead = 0;

while ((bytesRead = blobInputStream.read(binaryBuffer)) != -1)
{
outputFileOutputStream.write(binaryBuffer, 0, bytesRead);
totalBytesRead += bytesRead;
totalBytesWritten += bytesRead;
}

outputFileOutputStream.close();
blobInputStream.close();
inputFileInputStream.close();
blobOutputStream.close();
conn.commit();
rs.close();
stmt.close();
conn.close();
}
}

Can anyone tell me if I need to perform more checks on data integrity to prevent this loss of image at the bottom of the picture. It's almost like a spectrum of colour in a strip along the bottom.

Message was edited by:
mmanders
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2006
Added on Oct 19 2006
11 comments
1,586 views