Wednesday, 28 January 2015

Upload and Read Image In Memory in ADF

Some times we have requirement when ever user uplaod an image immediately he wants see the preview. So for this requirements  No need any servet class. Here I am Sharing code and process.

JSPX Code:

<af:panelGroupLayout id="pgl1">
          <af:inputFile label="Upload Image" id="if1" autoSubmit="true"
                              valueChangeListener="#{viewScope.MyBean.imageChangeEventInMemory}"/>
          <af:image source="/images/#{viewScope.MyBean.fileName}" id="i1"
                    partialTriggers="if1" inlineStyle="width:100px; height:100.0px;"/>
    </af:panelGroupLayout>

Managed Bean Code:

    public void imageChangeEventInMemory(ValueChangeEvent ve){
        UploadedFile myfile = (UploadedFile)ve.getNewValue();
        fileName = myfile.getFilename();
        FacesContext fctx = FacesContext.getCurrentInstance();
        ServletContext setvltctx =
            (ServletContext)fctx.getExternalContext().getContext();
        String imageDirPath = setvltctx.getRealPath("/");
        try {
            BufferedImage input = ImageIO.read(myfile.getInputStream());
            File OutputFile = new File(imageDirPath + "/images/" + fileName);
            ImageIO.write(input, "PNG", OutputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

private String fileName;

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getFileName() {
        return fileName;
    }


Note:
  You have to create images folder inside web contect folder, just creat empty folder.

No comments:

Post a Comment