I am trying to open oracle blobs and clobs to the screen in either Notepad or Wordpad. This code always works on the blob, sometimes on the clob and I can't figure out why it fails on certain resumes but not others. The data looks similar, some of the clobs are bigger than others, but I can't see anything specific to make it fail. I get no errors in my log, they just open with the text all ascii looking instead of readable text. If anybody has any ideas or areas for me to research, please let me know. I just now added the substring bit because I thought maybe some of the clobs were too big, but even 300 characters fail, so that's not it. Could there be something in the clob data itself that would blow this up when it is being converted to Wordpad? This is
java servlet to an oracle database.
Statement stmt = con.createStatement();
ResultSet rs;
InputStream is = null;
oracle.sql.BLOB blob = null;
oracle.sql.CLOB clob = null;
String name = "";
int pos =0;
int length=0;
OutputStream os = res.getOutputStream();
try{
query = " select attached_resume, "
+ " SUBSTR(candidate_resume, 1, 300) , "
+ " uploaded_resume_file_name "
+ " from candidate_resume "
+ " where candidate_id = '"+candidateId+"' ";
rs = stmt.executeQuery(query);
while(rs.next()){
blob = (BLOB) rs.getBlob(1);
clob = (CLOB) rs.getClob(2);
name = rs.getString(3);
}
if(Common.isNotEmpty(name)){ //resume is attached, open it
String ext = name.substring(name.indexOf(".")+1, name.length());
res.setContentType("application/"+ext+"");
res.setHeader("Content-Disposition","attachment;filename="+name);
is = blob.getBinaryStream();
byte []b = new byte[blob.getChunkSize()];
while ((length=is.read(b)) != -1){
pos += length;
os.write(b);
}
}else{ //no attached resume, try to open clob
res.setContentType("application/txt");
res.setHeader("Content-Disposition","attachment;filename="+candidateId);
is = clob.getStream();
byte []b = new byte[clob.getChunkSize()];
while((length=is.read(b)) != -1){
pos += length;
os.write(b);
}
}
}catch (SQLException se){
se.printStackTrace();
}finally{
is.close();
os.close();
}