aspose file tools*
The moose likes I/O and Streams and the fly likes Reading From a Binary Large Object(BLOB) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading From a Binary Large Object(BLOB)" Watch "Reading From a Binary Large Object(BLOB)" New topic
Author

Reading From a Binary Large Object(BLOB)

Robb Glietz
Greenhorn

Joined: Nov 08, 2001
Posts: 1
I am writting a text file into a BLOB, when I go to read it back out there are square boxes around my text.
Also I am only able to read it by using methods found in the class ByteArrayOutputStream. When I try to read it using the class ObjectInputStream all I get is the numbers which I guess are object refer numbers.
The code below will be the insert and the read along with the result which I am getting. I have left out the connection part simple because I know that is working since I do have information within the database.
Here is the code:
INSERT PART:
private void insertBLOB()
throws java.lang.Exception, java.sql.SQLException {
System.out.println("INSIDE insertBLOB");
BLOB blob = null;
File binFile = null;
BufferedInputStream bis = null;
FileInputStream fis = null;
OutputStream outstream = null;
String fileName = "forthrun";
String fileType = "byte";
String fileLocation ="C:\\test11.txt";
try
{
//Set AutoCommit to OFF - required by BLOB locking mechanism
connection.setAutoCommit(false);
//Create a statement
Statement stmt = connection.createStatement();
//Insert an empty BLOB locator
stmt.execute("INSERT INTO MEDIA_STORE VALUES('"+fileName+"','"+fileType+"',empty_blob())");
//excute the query and lock the BLOB row.
ResultSet rset = stmt.executeQuery("SELECT MDATA FROM MEDIA_STORE WHERE MNAME ='"+fileName+"' FOR UPDATE");
rset.next();
//get the BLOB locator
blob = ((OracleResultSet) rset).getBLOB(1);
//Get the large binary media file
int size;
binFile = new File(fileLocation);
fis = new FileInputStream(binFile);
System.out.println("FILEINPUTSTREAM BYTES= " + size = fis.available());
bis = new BufferedInputStream(fis);
//insert to the BLOB from an output stream
outstream = blob.getBinaryOutputStream();
//Read the input stream and write the output stream by chucks
byte[] chunk = new byte[blob.getChunkSize()];
int i = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

while ((i = bis.read(chunk)) != -1)
{
oos.writeObject(chunk);

}
byte [] bytes = baos.toByteArray();

outstream.write(bytes);
outstream.flush();

//close the input and output stream
bis.close();
outstream.close();

//close the statement
stmt.close();
System.out.println("DONE");
} catch (SQLException e)
{
e.printStackTrace(System.out);
}
} //end void
READ PART:
public void readBLOB()
throws java.io.IOException, java.sql.SQLException, java.lang.ClassNotFoundException {
System.out.println("INSIDE readBLOB");


System.out.println("variables");
String strStatement = null;
Statement stmt = connection.createStatement();
BLOB blob = null;
ResultSet rs = null;
FileInputStream instream = null;
ObjectInputStream objectinputstream = null;
ByteArrayOutputStream byteOut = null;
BufferedInputStream bis = null;
//String inobject="";
String fileName ="forthrun";
try {
System.out.println("select the blob locator from the database");
rs = stmt.executeQuery("SELECT MDATA FROM MEDIA_STORE WHERE MNAME ='"+fileName+"'");
rs.next();
System.out.println("get the BLOB locator");
blob = ((OracleResultSet) rs).getBLOB(1);
int length =(int) blob.length();
System.out.println("Length of blob is = " + length);
InputStream is = blob.getBinaryStream();
byteOut = new ByteArrayOutputStream();
int size = blob.getBufferSize();
System.out.println("size blob = "+ size);

byte[] buffer = new byte[size];
int read = 0;
while((read = is.read(buffer)) !=-1){
System.out.println("inside while for read");
byteOut.write(buffer,0,read);
byteOut.writeTo(System.out);//this is what prints out content right now..
is.close();
}
byte[] bytes = byteOut.toByteArray();
ByteArrayInputStream bytein = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bytein);

Object c = ois.readObject();

System.out.println("c= "+c);




} catch (Exception ex) {
ex.printStackTrace(System.out);
}




}//end void
RESULT PART:
INSIDE main
INSIDE openSession
INSIDE readBLOB
variables
select the blob locator from the database
get the BLOB locator
Length of blob is = 8159
size blob = 24396
inside while for read
T�This is a test to see what happens??T�
Thank you in advance for any help you can provide
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading From a Binary Large Object(BLOB)
 
Similar Threads
Convert java.io.File to ByteArryInputStream?
fileREader and FileWriter
Serialization issue
Not able to insert image more than 4K in Database
java.sql.SQLException: ORA-01407: