Friday, November 23, 2012

ADF : How to Upload a File?

How to Upload a File using ADF?
How to store image into database using ADF?





  

    public String commit() throws SQLException, IOException {
        img=(UploadedFile) if1.getValue();
        fileUpload(img,it1.getValue().toString(),it2.getValue().toString());
         return "view";
    }
   
   public String fileUpload(UploadedFile file, String fileId, String FileName) throws SQLException,
                                                     IOException {
       
            FileOperationAmDef = "model.fileOperation.am.FileOperationAM";
            FileOperationConfig = "FileOperationAMLocal";
            AM = Configuration.createRootApplicationModule(FileOperationAmDef,FileOperationConfig);
            ViewObject imgVO=AM.findViewObject("FileStoreView1");
            Row createImgRow = imgVO.createRow();
            imgVO.insertRow(createImgRow);
            createImgRow.setAttribute("FileId", fileId);
                    createImgRow.setAttribute("FileName", FileName);
                    createImgRow.setAttribute("FileContent", newBlobDomainForInputStream(file.getInputStream()));
            BindingContainer bindings = getBindings();
            AM.getTransaction().commit();
           
        return null;
        }
  
    private BlobDomain newBlobDomainForInputStream(InputStream is)
        throws SQLException, IOException{
            BlobDomain blbDmn = new BlobDomain();
            OutputStream os = blbDmn.getBinaryOutputStream();
            writeInputStreamToOutputStream(is, os);
            is.close();
            os.close();              
            return blbDmn;              
    }

    private static void writeInputStreamToOutputStream(InputStream in, OutputStream out)
        throws IOException{
            byte[] buffer = new byte[8192];
            int bytesRead = 0;
            while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
    }

 public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }