p:fileUpload question

UI Components for JSF
Post Reply
burferd
Posts: 234
Joined: 01 May 2010, 16:15

14 Jun 2010, 05:21

Using PrimeFaces2.0.1, JSF2, facelets, Glassfish3, NetBeans 6.9.

The application I am working on requires the ability to upload a file from the client to the server and load it into a database and fill a graphicImage with the image from the upload.

The code below gives an idea of the logic I currently use for displaying the image - the database upload is similar.
Basically, I grab the input stream from the input file on the client passed by the ActionEvent.getSource() method to populate the database and image.

I have 2 questions:
1) Can I fetch the input stream from the input file reference obtained by fetching it from the FileUploadEvent.getFile() method to fetch the input stream from the file on the client?
2) When the file us uploaded to the server, where does it get stored? I don't see any reference to the destination of the uploaded file in the examples or an upload directory in either the showcase example or the documentation. Do I need to physically write it to a destination if I want to save it on the server? I need the ability to upload the file to a user specified directory as well.
Something in the documentation to give an example of saving the file to a specific location on the server would be helpful.

Thanks.

Code: Select all

    InputFile inputFile;
    public void action(ActionEvent event){
        inputFile =(InputFile) event.getSource();
        //file has been saved
        if (inputFile.getStatus() == InputFile.SAVED) 
        {
            try
            {
                int size = (int)file.length();
                String totalPath = file.getAbsolutePath();
                ImagePlus imageP = new ImagePlus( totalPath );
                int height = imageP.getHeight();
                int width = imageP.getWidth();
                esb.loadImageToDb( totalPath, width, height, size, type );
                lsb.setMessage1( "Image has been loaded into database." );
                lsb.setImageBaseSize( width, height, 2, 391, 120, 90 );
                lsb.setImageFileUrlFull( totalPath );
                InputStream in = new FileInputStream(totalPath);
                byte[] data = new byte[size];
                in.read(data);
                lsb.setImageData(data);
                if (state != null) state.render();
                PCUtil.deleteDirFilesOnExit(pathName);
            }
            catch(Exception e )
            {
                String s = e.getMessage();
                lsb.logMsg( "<UploadSessionBean.action> Error on image upload: "+s);
            }
            componentStatus = "SAVED";
        }
        //invalid file, happens when clicking on upload without selecting a file, or a file with no contents.
        if (inputFile.getStatus() == InputFile.INVALID) {
        componentStatus = "INVALID";
        }
        //file size exceeded the limit
        if (inputFile.getStatus() == InputFile.SIZE_LIMIT_EXCEEDED) {
        componentStatus = "SIZE_LIMIT_EXCEEDED";
        }
        //indicate that the request size is not specified.
        if (inputFile.getStatus() == InputFile.UNKNOWN_SIZE) {
        componentStatus = "UNKNOWN_SIZE";
        }
    }
Using PrimeFaces 3.4, Mojarra 2.1.6, Glassfish 3.1.2, NetBerans 7.2, Hibernate 3.2.5 (sometimes)
Windows 7.

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

14 Jun 2010, 10:18

Hello,

You can configure upload directory in the web.xml if you want (see documentation to FileUploadFilter). Otherwise the directory is choosen automatically by Apache fileupload (user home directory or something like).

Your example is alike ICEFaces file upload with various states. Is it?
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

burferd
Posts: 234
Joined: 01 May 2010, 16:15

14 Jun 2010, 16:02

Oleg:

Thanks for the feedback.
I found a reference to an uploadDirectory parameter in the file upload filter section.
The description says the uploadDirectory is:
Disk repository path to keep temporary files that exceed the threshold size. By default it is System.getProperty ('java.io.tmpdir").
I guess the question now is whether or not all files will get written into that directory or just the ones that exceed the threshold size like the documentation says.

What I need is a way to write to different directories.
The Idea is to allow the user to upload support documentation to the server for future reference.
The document directory will have subdirectories based on the user id to hold the actual documents and the path to the documents will be stored in the database.

You guessed correctly, the current application uses ICEfaces.
I am trying to migrate from ICEfaces to PrimeFaces while moving from JSF1.2 to JSF2.
Using PrimeFaces 3.4, Mojarra 2.1.6, Glassfish 3.1.2, NetBerans 7.2, Hibernate 3.2.5 (sometimes)
Windows 7.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 47 guests