Tuesday, July 1, 2014

How to Upload file in ADF Application

Upload file functionality is implemented using af:inputFile component which provides user with a dialog to select the input file.

File selected can be accessed using vcl of the inputfile which provides the
org.apache.myfaces.trinidad.model.UploadedFile object of the selected file:

using the file object, user can get the attributes like fileName, InputStream etc as:

        UploadedFile file = (UploadedFile) event.getNewValue();
        if (file != null)
        {
          fileName=file.getFilename();
          try {
                inputstream=file.getInputStream();
          } catch (IOException e) {
                e.printStackTrace();
          }

     

User would use this inputstream to upload the file in any given location as(can be written as button action):
        OutputStream outputStream = null;
        try{
            outputStream = new FileOutputStream(new File("C:\\JDeveloper\\mywork\\uploadedFiles\\"+fileName));     //hard coded for now
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputstream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
            }
            System.out.println("Done!");
        }catch(Exception e){
            e.printStackTrace();
        }


Attached code can be downloaded from here


No comments:

Post a Comment