Monday 28 October 2013

Download BLOB File from Database

  Below code used to download BLOB File from database.

 Write Below Code Under Event ..


    String MimeType=null,FileName=null;
    String Rowref=pageContext.getParameter(EVENT_SOURCE_ROW_REFERENCE);
    OtherReportRequestVORowImpl row=(OtherReportRequestVORowImpl)am.findRowByRef(Rowref);
    try {
       String Query=" SELECT 'text/plain' MIME_TYPE, xxebex.xxebex_public.CLOB_TO_BLOB(REQUEST_LOG), 'InfoRequest_'||REQUEST_ID||'_ProcessLog.html' FILE_NAME"+
    " FROM xxebex.ebex_info_requests  WHERE request_id ='"+row.getRequestId()+"'";
    PreparedStatement cs=(PreparedStatement)am.getOADBTransaction().getJdbcConnection().prepareStatement(Query);
    ResultSet rs=cs.executeQuery();
    while(rs.next())
    {
        java.sql.Blob blob;
        blob = rs.getBlob(2);
        MimeType=rs.getString(1);
        FileName=rs.getString(3);
        int iLength = (int)(blob.length());
        System.out.println("Length:"+iLength);
        response.setHeader("Content-Disposition", "attachment; filename=\""+FileName+"\"");
        response.setContentType(MimeType);
        response.setContentLength(iLength);
        ServletOutputStream op = response.getOutputStream();
        op.write(blob.getBytes(1, iLength));
        op.flush();
        op.close();

    }
   
       
    }
    catch(Exception e) {
        System.out.println(e.getMessage());
    }