Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

BLOB throwing class cast exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to fetch a file from a table in database. The whole file is stored as a BLOB in the table. But I am getting a class cast exception.

Blob file = rs.getBlob("file_data");

//Getting CLASS CAST Exception in the next line

InputStream inputStream =((oracle.sql.BLOB) file).getBinaryStream();

byte data[] = new byte[((oracle.sql.BLOB)file).getChunkSize()];

Can anyone help me out whether its code problem or jar(driver) problem.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

if your concern is to read the content of column which datatype is BLOB,you may try the following code.

byte[] eventBytes = rs.getBytes("context_obj");
java.io.ObjectInputStream s = new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(b));
Object obj = s.readObject();
System.out.println("Context_obj :"+obj);

You can cast that object to HashMap or Hashtable or any class.

Rajesh
 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My requirement is to not just read the file but showing a popup for open/save for the file I am trying to retrieve and then be able to save the file in my desired location.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Traditionally, this is a clue that the class cast on that next line is incorrect. You cast to oracle.sql.BLOB - what makes you think that's the correct class? It probably isn't, and that's why you get the error. Instead, look at the declared return type of getBlob: java.sql.Blob. This interface has the getBinaryStream() method - isn't that enough? I'm not sure why you need the Oracle-specific method getChunkSize(); you should be able to work without it. Isn't your goal to get all of the data, regardless of chunk size? Probably you can either put it all in a single byte[] array (sized using getLength()) or copy the stream to some other OutputStream - e.g. a FileOutputStream or a servlet response's OutputStream.
 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am now using the following code:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","Attachment; Filename=ABC.xls");
OutputStream fileStream = null;
fileStream = response.getOutputStream();

Blob file = (Blob)rs.getBlob("file_data");
byte[] data = file.getBytes(3,(int)file.length());
InputStream inputStream = file.getBinaryStream();
byte[] jbuffer = new byte[10];

if(inputStream != null){
int length = 0;
long totalsize = 0;
while ((length = inputStream.read(jbuffer)) != -1) {
fileStream.write(jbuffer,0,length);

}
inputStream.close();
fileStream.flush();
fileStream.close();
}else{
throw new Exception(" data not found!");
}

Class cast exception is not coming now but still no popup is coming for downloading the file. what is going wrong here?
 
I knew that guy would be trouble! Thanks tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic