This discussion is archived
1 2 Previous Next 18 Replies Latest reply: Jan 22, 2013 2:14 AM by 985680 RSS

ADF 11g upload file

985680 Newbie
Currently Being Moderated
Hi
I am new to adf and I want to upload a file and store it in a specified directory + I want to save the path of this file in a database table.. I read a lot of blogs and tutorials and I am not able to find any solution
any help ?
  • 1. Re: ADF 11g upload file
    Santosh M E Explorer
    Currently Being Moderated
    Hi,

    have a look to below link,

    http://docs.oracle.com/html/E1874501/devguide/fileUpload.html
    _http://technology.amis.nl/2007/07/14/adf-faces-file-uploading-it-is-really-that-simple/                                                                                                                                                                                                                                                                                                                                                                                           
  • 2. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    Hi

    Thanks for your help , I read these articles before and I think they dont answer me .. I want step by step tutorial. Do u know anything like that ?
    In all cases , thanks again
  • 3. Re: ADF 11g upload file
    Timo Hahn Oracle ACE
    Currently Being Moderated
    There is no step by step tutorial for every problem...
    Reading the blogs and tutorials should give you enough information to code the remaining part yourself.
    You can read my blog on handling images http://tompeez.wordpress.com/2011/11/26/jdev11-1-2-1-0-handling-imagesfiles-in-adf-part-1/ (and the other tow parts). Then you have to change the code to store the data on the servers file system and the path to it in the db.
    However, I would not encourage you to store the data in the file system as you can't guaranty that the files are not delete, leaving stale db records.

    Timo
  • 4. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    Thanks for the helpfull answer and advice

    cheers
  • 5. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    InputStream inputStream = myfile.getInputStream();
    BufferedImage input = ImageIO.read(inputStream);

    File outputFile =
    new File(imageDirPath + "/screenshots/"+myfile.getFilename());
    ImageIO.write(input, "PNG", outputFile);

    this code is taken from a java class to upload and save an image using adf ( and saving the file on the disk ).. I am trying to edit the code as I want.. but I want to upload a file and not an image.. what is the equivelant of this code above if I am using files and not images.

    thank you
  • 6. Re: ADF 11g upload file
    Timo Hahn Oracle ACE
    Currently Being Moderated
    You can use Apache Commons FileUtils (http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html)
     copyInputStreamToFile(InputStream source,
                                             File destination);
    Your code should look like
    InputStream inputStream = myfile.getInputStream();
    BufferedImage input = ImageIO.read(inputStream);
    String filepath = "/tmp";
    File outputFile =
    new File(filepath + myfile.getFilename()); 
    FileUtils.copyInputStreamToFile(inputStream ,
                                             outputFile );
    Timo

    Edited by: Timo Hahn on 21.01.2013 15:18
  • 7. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    thanks timo.
    this code works for an image. I want the code to work not only for images, but for all kinds of files ( pdfs, docx , ... )

    any help ?

    thanks
  • 8. Re: ADF 11g upload file
    Timo Hahn Oracle ACE
    Currently Being Moderated
    Sorry, forgot to delete some of your code...
    I edited my previous post.

    Timo
  • 9. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    BufferedImage input = ImageIO.read(inputStream);

    what do i use instead of this ?
  • 10. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    thank agaim timo ,

    the code is not compiling and it said copyInputStreamToFile does not exist.
    i am importing org.apache.commons.io.*;

    what should i import ?


    thanks
  • 11. Re: ADF 11g upload file
    Timo Hahn Oracle ACE
    Currently Being Moderated
    Have loaded the commons io version 2.4 from http://commons.apache.org/io ? There I found the method http://commons.apache.org/io/api-release/index.html?org/apache/commons/io/package-summary.html

    Timo
  • 12. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    java class
    ----------------------------------------------------------------------------------------------------------------------------

    package view.backing;



    import org.apache.commons.io.FileUtils;

    import java.io.File;

    import java.io.InputStream;

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;

    import javax.faces.context.FacesContext;

    import javax.servlet.ServletContext;
    import org.apache.myfaces.trinidad.model.UploadedFile;



    @ManagedBean(name="backing_uploadfile")
    @RequestScoped
    public class UploadFile {
    public UploadFile() {
    }
    private UploadedFile cv_file ;

    public void setUploadFile(UploadedFile cv_file) {
    this.cv_file = cv_file;
    }

    public UploadedFile getUploadFile() {
    return cv_file;
    }

    public String processUpload() {
    UploadedFile myfile = this.getUploadFile();
    String filetype = myfile.getContentType();
    String fileExt = filetype.substring(filetype.lastIndexOf("/")+1);
    if(fileExt.equals("pdf") || fileExt.equals("docx"))
    {
    FacesContext fctx = FacesContext.getCurrentInstance();
    ServletContext servletCtx =
    (ServletContext)fctx.getExternalContext().getContext();
    String cvDirPath = servletCtx.getRealPath("/");
    try {

    InputStream inputStream = myfile.getInputStream();
    String filepath = "/tmp";
    File outputFile =
    new File(filepath + myfile.getFilename());
    FileUtils.copyInputStreamToFile(inputStream , outputFile );



    } catch (Exception ex) {
    ex.printStackTrace();
    }
    JSFUtils.addFacesInformationMessage("Successfully uploaded: " + myfile.getFilename() +
    " (" + myfile.getLength() + " bytes)");
    // pnlUploadForm.setVisible(false);
    }
    else
    {
    JSFUtils.addFacesInformationMessage("You can only upload .pdf and .docx files!");
    }

    return null;
    }



    }



    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    jspx page



    <?xml version='1.0' encoding='UTF-8'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
    <af:document title="createstep3.jspx" id="d1">
    <af:form id="f1" usesUpload="true">
    <af:panelBorderLayout id="pbl1">
    <f:facet name="start">
    <af:panelGroupLayout id="pgl1" layout="horizontal" halign="center">
    <af:image source="#{resource['images:cv-upload.png']}" shortDesc="/upload.png" id="i1"
    inlineStyle="height:18.4em; width:15em;"/>
    <af:inputFile label="CV" id="if1"
    value="#{uploadbean.file}"/>
    <af:commandButton text="upload" id="cb3" action="#{backing_uploadfile.processUpload}"/>
    </af:panelGroupLayout>
    </f:facet>
    <f:facet name="bottom">
    <af:panelGroupLayout id="pgl2" layout="horizontal">
    <af:commandButton text="Cancel" id="cb1" action="cancel" immediate="true"/>
    <af:trainButtonBar value="#{controllerContext.currentViewPort.taskFlowContext.trainModel}"
    id="tbb1"/>
    <af:commandButton text="Save" id="cb2" action="success"/>
    </af:panelGroupLayout>
    </f:facet>
    <f:facet name="end"/>
    <f:facet name="top">
    <af:train value="#{controllerContext.currentViewPort.taskFlowContext.trainModel}" id="t1"/>
    </f:facet>
    </af:panelBorderLayout>
    </af:form>
    </af:document>
    </f:view>
    <!--oracle-jdev-comment:preferred-managed-bean-name:uploadbean-->
    </jsp:root>




    I can't upload the file.. whats is wrong with my code ?
  • 13. Re: ADF 11g upload file
    Timo Hahn Oracle ACE
    Currently Being Moderated
    What do you mean by
    I can't upload the file..
    Do you get an error? If yes which one?
    Is your code called? have you used a debugger to step through the code?

    Timo
  • 14. Re: ADF 11g upload file
    985680 Newbie
    Currently Being Moderated
    the compilation succeed and there is no errors when I run the jspx page... only the image appears on the page( blank page with an image inside it ( the image I am using it in my design ) )


    thanks again

    Edited by: HUFA on Jan 21, 2013 7:23 AM
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points