Image upload in ADF

22 篇文章 0 订阅
14 篇文章 0 订阅

Image upload in ADF

In this post we will see how to upload images in Oracle ADF application.

 To upload a file don't forget to set UsesUpload property of form of jspx page to true.








Lets start with uploading a image/file from client to the server.
ADF rich faces provide the tag af:inputFile to allow uploading of data.  
Define a variable Type UploadedFile and create its accessors

private UploadedFile _file;

public void setFile(UploadedFile _file) {
     this._file = _file;
}

public UploadedFile getFile() {
      return _file;
    }
And bind it with inputFile component's value property

<af:inputFile label="Upload Image" id="if1" value="#{ImageUploadBean.file}" />



 In this we will see 2 places where we can uploading image depending on our requirement :-
1.) In Database(BLOB)  :-
In this part, we will see how to upload a image/file from the client to and store image in a blob column in a database.

We have used button action to upload file

Following are codes to upload Image in database blob column  

    public String UploadImageAction() {
        UploadedFile myfile = this.getFile();
         
        
        if (myfile != null) {
             
            if (myfile.getContentType().equalsIgnoreCase("image/jpeg") ||                 myfile.getContentType().equalsIgnoreCase("image/png")  || myfile.getContentType().equalsIgnoreCase("image/gif")
                ){

                AppImageAMImpl am = (AppImageAMImpl)resolvElDC("AppImageAMDataControl");
                ViewObjectImpl v = am.getAppImgAdd();
                AppImgAddVORowImpl currRow = (AppImgAddVORowImpl)v.getCurrentRow();
                                                               
                try {
                     
                     
                    //to save image in Blob column in database directory..............

                                        
                    currRow.setImgBlob(createBlobDomain(getFile()));                                       


                } catch (Exception ex) {
                     
                }
            }  
        }
        setFile(null);
        return null;
    }


    private BlobDomain createBlobDomain(UploadedFile file) {

        InputStream in = null;
        BlobDomain blobDomain = null;
        OutputStream out = null;

        try {
            in = file.getInputStream();

            blobDomain = new BlobDomain();
            out = blobDomain.getBinaryOutputStream();
            byte[] buffer = new byte[8192];
            int bytesRead = 0;

            while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            in.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.fillInStackTrace();
        }

        return blobDomain;
    }

2.) In Actual path in server  :-

 In this part, we will see how to upload a image/file from the client to actual location in server.

        public String UploadImageAction() {
        UploadedFile myfile = this.getFile();        
        
        if (myfile != null) {
             
            if (myfile.getContentType().equalsIgnoreCase("image/jpeg") ||  myfile.getContentType().equalsIgnoreCase("image/png")
                || myfile.getContentType().equalsIgnoreCase("image/gif")
                ){

                String path = "D:\\Images\\";

                 if(myfile.getContentType().equalsIgnoreCase("image/jpeg")){
                     
                    TypeVal=".jpeg";
                }
                else if(myfile.getContentType().equalsIgnoreCase("image/png")){
                      
                     TypeVal=".png";
                }
                 
                else if( myfile.getContentType().equalsIgnoreCase("image/gif")){
                     
                    TypeVal=".gif";
                }
                 
                AppItemImageAMImpl am = (AppItemImageAMImpl)resolvElDC("AppItemImageAMDataControl");
                ViewObjectImpl v = am.getAppItmImgAdd();
                AppItmImgAddVORowImpl currRow = (AppItmImgAddVORowImpl)v.getCurrentRow();

                String ImgId = currRow.getImgId();
                 
                try {
                     
                    InputStream inputStream = myfile.getInputStream();
                    BufferedImage input = ImageIO.read(inputStream);

                    //to save image in another directory..............

                    File outputFile = new File(path + ImgId + TypeVal);
                    ImageIO.write(input,type, outputFile);                                       
                    
                } catch (Exception ex) {
                    // handle exception
                  
                }
            }  
        }
        setFile(null);
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值