Thursday, November 22, 2012

ADF : How to Download a file ?

How to Download a file?
How to fetch the file/images from database?








public void viewImage (FacesContext context, OutputStream os) throws IOException
{
try {



HttpServletRequest request =
(HttpServletRequest)context.getCurrentInstance().getExternalContext().getRequest();
HttpSession session = request.getSession(false);
int imgId =
((Integer)session.getAttribute("imgId")).intValue();
HttpServletResponse response =
(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
os = response.getOutputStream();
String amDef = "com.neel.img.am.ImageAM";
String config = "ImageAMLocal";
ApplicationModule am =
Configuration.createRootApplicationModule(amDef, config);
ViewObjectImpl vo =
(ViewObjectImpl)am.findViewObject("ImageQueryVO1");

vo.setNamedWhereClauseParam("imgId",
(new Integer(imgId)).toString());
vo.setNamedWhereClauseParam("imgDocId", "1");
vo.executeQuery();
Row imgRow = vo.first();



BlobDomain image = (BlobDomain)imgRow .getAttribute("ImgContent");



String fileName = (String)imgRow .getAttribute("FileName");

String fileType = (String)imgRow.getAttribute("FileExt");
response.reset();
response.setContentType("image/jpeg");
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName);
InputStream is = image.getInputStream();
// copy blob to output
byte[] buffer = new byte[10 * 1024];
int nread;
while ((nread = is.read(buffer)) != -1)
os.write(buffer, 0, nread);
os.close();
vo.setWhereClause(null);
Configuration.releaseRootApplicationModule(am, false);



return ;
} catch (Exception e) {
e.printStackTrace();
return ;
}


}


SELECT img_content, file_name, file_ext, img_doc_mast_id FROM ops_img_doc_dtls WHERE claim_id=:imgId AND img_doc_mast_id=:imgDocId

        Name="imgDocId"
      Kind="where"
      Type="java.lang.String"/>
         Name="imgId"
      Kind="where"
      Type="java.lang.String"/>