Hello!
To deal with Oracle BLOB-field, you have to use the the Oracle-type BLOB
and Oracle's ( OracleResultSet ). To retrieve from db only using
getBinaryStream() (ResultSet )does not work for me.
Here is the code that works for me! It's all stolen from OTN.
I use Jason Hunters com.oreilly.servlet.MultipartRequest to do the
file-uploading. ( It gives me the file-stream, and it goes directly to
the db-BLOB )
Hope it helps, Bjarte :-)
package sportservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import sportdb.*;
public class ImageGenerator extends SportServlet {
Connection con = null;
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setHeader("pragma", "no-cache");
response.setHeader("content-encoding","image/gif");
response.setContentType("image/gif");
BufferedOutputStream out = new BufferedOutputStream
(response.getOutputStream());
InputStream blobStream = null;
try {
con = pool.getConnection();
blobStream = SportDBUtils.getTeamLogo("10002", con );
byte[] l_buffer = new byte[10]; // buffer holding bytes to be
transferred
int l_nbytes = 0; // Number of bytes read
while ((l_nbytes = blobStream.read(l_buffer)) != -1) // Read from
BLOB stream
out.write( l_buffer, 0, l_nbytes );
out.flush();
blobStream.close();
}catch ( SQLException e ) { System.out.println("Error: " + e ); }
finally {
if ( con != null ) pool.returnConnection(con);
}
--------------------------SportDBUtils------------------------
//Package for Oracle Extensions to JDBC
import oracle.sql.*;
import oracle.jdbc.driver.*;
public static InputStream getTeamLogo( String teamId, Connection acon
) {
InputStream blobStream = null;
try {
// Open a stream to read the BLOB data
PreparedStatement ps = acon.prepareStatement("SELECT ID, LOGO FROM
TEAM WHERE ID = ?");
ps.setString(1, teamId);
ResultSet rs = ps.executeQuery();
BLOB logoBlob = null;
while( rs.next() ) {
logoBlob = ((OracleResultSet)rs).getBLOB(2);
}
ps.close();
blobStream = logoBlob.getBinaryStream();
return blobStream;
/*// Open a file stream to save the BLOB data to file
FileOutputStream l_fileOutStream = new
FileOutputStream("c:/bjarte.bmp");
// Read from the BLOB data input stream, and write to the file
output
// stream
byte[] l_buffer = new byte[10]; // buffer holding bytes to be
transferred
int l_nbytes = 0; // Number of bytes read
while ((l_nbytes = blobStream.read(l_buffer)) != -1) // Read from
BLOB stream
l_fileOutStream.write(l_buffer,0,l_nbytes); // Write to file
stream
// Flush and close the streams
l_fileOutStream.flush();
l_fileOutStream.close();
blobStream.close();
*/
} catch (Exception ex) { // Trap SQL and IO errors
}
return blobStream;
}
public static void saveTeamLogo( String teamId, String filename,
Connection acon ) {
try {
acon.setAutoCommit(false);
// Form a SQL statement for inserting a row into
AIRPORT_LOB_DETAILS
// The LOB column values are initialized to empty in this step,
and
// will be loaded in the steps below.
PreparedStatement ps = acon.prepareStatement(
"INSERT INTO TEAM ( ID, SPORT_ID, LOGO ) VALUES ( ?, 1,
empty_blob() )");
ps.setInt(1, 10000 );
ps.execute();
ps.close();
// Retrieve the row just inserted, and lock it for insertion of
the
// LOB columns.
Statement stmt = acon.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT ID, LOGO FROM TEAM WHERE ID = 10000 FOR UPDATE");
// Retrieve BLOB and CLOB streams for AIRPORT_MAP and
AIRPORT_SUG_BOOK
// columns, and load the sample files
if ( rs.next()) {
// Get the BLOB locator and open output stream for the BLOB
BLOB logoBLOB = ((OracleResultSet)rs).getBLOB(1);
OutputStream blobOutputStream =
logoBLOB.getBinaryOutputStream();
// Open the sample file as a stream for insertion into the BLOB
column
File logoFile = new File( filename );
InputStream logoFileStream = new FileInputStream( logoFile );
// Buffer to hold chunks of data to being written to the BLOB.
// In Oracle8.1.5 JDBC drivers a method getBufferSize() is
available
// in the BLOB class, that returns the optimal buffer size
byte[] buffer = new byte[10 * 1024];
// Read a chunk of data from the sample file input stream, and
write the
// chunk to the BLOB column output stream. Repeat till file has
been
// fully read.
int nread = 0; // Number of bytes read
while (( nread= logoFileStream.read( buffer )) != -1) // Read
from file
blobOutputStream.write( buffer, 0, nread ); // Write to BLOB
// Close both streams
logoFileStream.close();
blobOutputStream.close();
}
acon.commit();
// Close Result Set and statement
rs.close();
stmt.close();
} catch (Exception ex) {
System.out.println( ex );
}
}
Last : to deliver image and text to a browser do this :
public class MySerlvet {
doGet {
setContentType( text/html );
out.println("<html>");
out.println("<body>");
out.println("<img
src=\"/serlvet/packagename.ImageGenerater?id=12345\">");
out.println("I like servlet-programming!!!!")
out.println("</body>");
out.println("<html>");
out.flush();
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html