Wednesday, April 28, 2010

ADFway of rendering a BLOB from a Servlet

Say, you have a BLOB in one of your DB table column.
You want to stream the BLOB onto your ADF page.How to do it?

The ViewOBject contains 4 columns.
PrimaryKey,FileName,MimeType and Blob

Here is the servlet code involving ADFbc.

public ApplicationModule getHPApplicationModuleFromPool(){
//Get the ApplicationModule Client Interface and get the MyVO
String amDef = "mypackage.applicationModule.MyAM";
String amConfig = "MyAMLocal";
ApplicationModule am =
Configuration.createRootApplicationModule(amDef, amConfig);

return am;

}catch(Exception e){

return null;


public void blobProcessing(String primaryKey) {
ApplicationModule am = null;
//Get the Row for the Passed TopicId
Key key = new Key(new Object[]{primaryKey});
am = getHPApplicationModuleFromPool();
ViewObject vo = am.findViewObject("MyVo");

Row row = vo.getRow(key);
if(topicRow != null){
String fileName = (String)row.getAttribute("FileName");
mimeType = (String)row.getAttribute("MimeType");
//Sets the MimeType
response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
//Sets the content disposition style
response.addHeader("Content-Disposition", "inline; filename="+ fileName);
//Read the row.getBlob() and redirect to an outPutStream
BlobDomain blobContent = (BlobDomain)row.getAttribute("Blob");
//gets the ByteArray from BlobDomain
byte[] bytearray = topicContent.toByteArray();
//gets the Binary InputStream from BlobDomain
InputStream reader = blobContent.getBinaryStream();;
int size = 0;
//Read the InputStream and write to Servlet's output Stream.
while ((size = != -1)
//Close the reader stream
Configuration.releaseRootApplicationModule(am, false);
}//End Blob processing

}catch(Exception e){
if(am != null)
Configuration.releaseRootApplicationModule(am, false);
}catch(Exception e1){

No comments: