Question: I want to load a photograph image from
my client PC into an Oracle table. I know that utl_file cannot read files
on a client PC, but I need to transfer the photograph BLOB image into Oracle.
What are the options for loading images from a PC into a table on a remote
server?
Answer: This is a very common function and each programmer will
approach it differently. Remember, the Oracle SQL*Net "client" is
disconnected from direct access to the Oracle server, and the server can only
communicate to the client as you define it.
 |
For immediate expert assistance in Java graphics for images in
Oracle, just call us or e-mail
. |
A risky approach to moving images into Oracle
This forum thread
shows PL/SQL stored procedures for loading images from a client side
application, but there remains the issue of moving the BLOB from the remote
client to the server, where PL/SQL can get it. If you want to store the
BLOB image on the server, you must write code to either "push" or "pull" it, not
a good approach:
- RISKY - PUSH BLOB - Use an FTP
program to transfer the BLOB to a known directory on the server. After FTP
is complete, invoke PL/SQL to load it.
- RISKY - PULL BLOB- Have the server
open a secure connection to the client disk as a shared device. Once the
client-side disk is mounted, PL/SQL can reach it.
- Use TNS - The first two approaches are risky and create
unnecessary exposures. By storing an empty BLOB table column, and then
re-selecting it FOR UPDATE, you can transfer the LOB image from the PC into
Oracle without opening any external connections.
The best approach for moving images from a PC to Oracle
As we note, it is easy to move-in a photograph of other LOB image, by storing
an empty BLOB table column, and then re-selecting it FOR UPDATE, streaming the
image into the BLOB column, and then storing it with a COMMIT.
To load images from a client PC, a programmer often uses
Perl
interfaces for BLOB's or
Java OCI
programs for loading LOB's. Regardless of the language chosen, a basic
method for storing BLOB's from a PC device (such as a PC camera) include these
steps.
1. Get a connection
2. Create a statement object.
3. Insert an empty row in the table.
4. Select the temp blob back out of the table "FOR UPDATE".
5. Stream the blob content into the temp blob.
6. Commit.
Below is some psuedocode to save an image from a PC client onto a pictures
table in Oracle, with the image table having only a primary key and a blob
column. If you don't want to write your own BLOB loading program, you can
hire experts to create a custom program to capture and upload images from a
client PC into an Oracle table, just contact us at
.
public boolean saveImageToDatabase(Image
image, int pri_key)
{
if (!createEmptyBlobRow(pri_key)) return false;
BLOB tempBlob = getTempBlob(pri_key);
if (tempBlob == null) return false;
if (!writeBlobToDatebase(tempBlob)) return false;
return true;
}
private boolean createEmptyBlobRow(int pKey)
{
try
{
stat.executeUpdate("insert into x_pictures" +
call BC for complete script
" ( " + pKey + "," + EMPTY_BLOB()");
return true;
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
}
return false;
}
private BLOB getTempBlob(int pKey)
{
try
{
rs = stat.executeQuery("select blob_content from x_pictures" +
call BC for complete script
if (rs.next()) return ((OracleResultSet) rs).getBLOB("blob_content");
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
}
return null;
}
private boolean writeBlobToDatebase(BLOB t_Blob)
{
try
{
int chunksize = t_Blob.getChunkSize();
byte[] byteBuffer = new byte[chunksize];
call BC for complete script
long position = 1;
int bytesRead;
while ((bytesRead = b_in.read(byteBuffer)) != -1)
{
t_Blob.putBytes(position,byteBuffer);
position += bytesRead;
}
stat.execute("COMMIT");
return true;
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
catch (IOException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
}
}
|

|