1 Reply Latest reply on Oct 23, 2013 3:29 PM by valatharv

    Rich:FileUpload doesnt work

    chsmcol

      Hi everyone,

       

      I'm new developing Applications using JSF with Richfaces. I've been trying to use the fileUpload (This is my first time using this component) to upload pictures for users and later i needed for PDFs, etc., but i cant get it to work. What i want to do is get the picture from the client-pc and copy it into a server folder for further use. I'm using windows platform, so i was trying to get it from the temp folder but it's not there or maybe I don't know the path for it. So then I try to use the other option setting createTempFiles to false but I cant get it to work either.


      I'm following the example in RichFaces Showcase website which calls a bean method as fileUploadListener="#{userController.pictureUpload()}", but in the javabean the method has an input parameter as  public void pictureUpload(FileUploadEvent event) throws Exception, so my pictureUpload method is not recognize in my Users.xhtml file. My first question is what's the right way to setup the view (Users.xhtml) so that the bean method is called property.


      Also, I did some research and find a java function to copy files from one path to another. The function is: copy(Path, Path, CopyOption). So all I need is a way to get the path of the uploaded file to use it. That's why I was trying to use the temp folder option. Any ideas how to do this?


      If you guys have a better way to accomplish this, please advise. Like i said I'm new when it comes to uploading files.


      Your Help will be appreciated.

      Thanks...

        • 1. Re: Rich:FileUpload doesnt work
          valatharv

          Hi Christian,

           

          1) I think item.getFile() will tell the path... I believe temp files are created under ..C:\Users\<username>\AppData\Local\{something like... upload

          But, make sure you are not deleting the file after upload

           

          2) Here is the code which works for me...

          a) FileUploadHome listerner function

          b) Wrapper class

          c) XHTML


          import java.io.BufferedInputStream;

          import java.io.BufferedReader;

          import java.io.ByteArrayInputStream;

          import java.io.File;

          import java.io.FileInputStream;

          import java.io.InputStreamReader;

          import java.util.ArrayList;

          import java.util.Date;

          import java.util.Iterator;

          import java.util.List;

          import java.util.StringTokenizer;

          import org.apache.commons.io.FileUtils;

          import org.apache.commons.io.FilenameUtils;

          import org.hibernate.validator.InvalidStateException;

          import org.hibernate.validator.InvalidValue;

          import org.jboss.seam.ScopeType;

          import org.jboss.seam.annotations.In;

          import org.jboss.seam.annotations.Logger;

          import org.jboss.seam.annotations.Name;

          import org.jboss.seam.annotations.Out;

          import org.jboss.seam.framework.EntityHome;

          import org.jboss.seam.log.Log;

          import org.richfaces.event.UploadEvent;

          import org.richfaces.model.UploadItem;


          @Name("fileUploadHome")

          public class FileUploadHome extends EntityHome<FileUpload> {

           

            @In(create = true)

            ItemHome itemHome;

           

            private FileWrapper fileWrapper= new FileWrapper();

           

            private int uploadsAvailable = 5;

              private boolean autoUpload = false;

              private boolean useFlash = false;

              private Date timeStamp= new Date();

           

              public int getSize() {

                  if (fileWrapper.getFileList().size()>0){

                      return fileWrapper.getFileList().size();

                  }else

                  {

                      return 0;

                  }

              }

           

              @In(scope=ScopeType.SESSION,required=false)

            @Out(scope=ScopeType.SESSION, required=false)

            FileUpload file;

           

              public void listener(UploadEvent event) throws Exception{

                  UploadItem item = event.getUploadItem();

                  file = new FileUpload();

                  file.setFileName(FilenameUtils.getName(item.getFileName()));  

                  file.setFileNameAndPath(item.getFile());

                  file.setFileLength(item.getFileSize());

           

           

            //This should show the path..

            //On windows it should be  C:\Users\<username>\AppData\Local\{something like... upload

                  log.info("FileUploadHome.listener item.getFile() : "+item.getFile());

            

                  //Add Content type

                  String fileType;

                  int extDot = item.getFileName().lastIndexOf('.');

            if(extDot > 0){

            String extension = item.getFileName().substring(extDot +1);

           

            if("csv".equals(extension)){

            fileType="text/csv";

            } else {

            fileType = "application/unknown";

            }

            file.setFileType(fileType);

            file.setFileUploadDate(new java.util.Date());

            }

           

            fileWrapper.getFileList().add(file);

              uploadsAvailable--;

              itemHome.getInstance().setFileUpload(new ArrayList<FileUpload>(fileWrapper.getFileList()));

           

           

            if (item.isTempFile()) {

            deleteTempFile(item.getFile());

            }

           

              this.getFileWrapper().clear();

            this.fileWrapper.getFileList().clear();

            super.getInstance().setItem(null);

            this.file=null;

            this.clearInstance();

            log.info("FileUploadHome.listener leave....");  

              }

           

              public long getTimeStamp(){

                  return System.currentTimeMillis();

              }

           

              public boolean isUseFlash() {

                  return useFlash;

              }

           

           

              public void setUseFlash(boolean useFlash) {

                  this.useFlash = useFlash;

              }

           

              public boolean isAutoUpload() {

                  return autoUpload;

              }

           

           

              public void setAutoUpload(boolean autoUpload) {

                  this.autoUpload = autoUpload;

              }

           

              public int getUploadsAvailable() {

                  return uploadsAvailable;

              }

           

           

              public void setUploadsAvailable(int uploadsAvailable) {

                  this.uploadsAvailable = uploadsAvailable;

              }

           

              public String clearUploadData() {

              fileWrapper.clear();

              setUploadsAvailable(uploadsAvailable);

                  return null;

              }

           

              public FileWrapper getFileWrapper() {

            return fileWrapper;

            }

           

           

            public void setFileWrapper(FileWrapper fileWrapper) {

            this.fileWrapper = fileWrapper;

            }

           

           

            private void deleteTempFile(File f) throws SecurityException{

            f.delete();

              }

          }

           

           

          import java.io.File;

          import java.io.Serializable;

          import java.util.ArrayList;

          import java.util.List;

          import javax.persistence.EntityManager;

          import org.jboss.seam.ScopeType;

          import org.jboss.seam.annotations.In;

          import org.jboss.seam.annotations.Name;

          import org.jboss.seam.annotations.Scope;

           

           

          @Name("fileWrapper")

          public class FileWrapper implements Serializable {

           

            private static final long serialVersionUID = 8007584769756527350L;

            private List<FileUpload> fileList = new ArrayList<FileUpload>();

            private boolean complete;

           

            public void setFileList(List<FileUpload> fileList) {

            this.fileList = fileList;

            }

           

           

            public List<FileUpload> getFileList() {

            return fileList;

            }

           

            public void clear() {

            fileList.clear();

            }

           

           

            public void setComplete(boolean complete) {

            this.complete = complete;

            }

           

           

            public boolean isComplete() {

            return complete;

            }

          }

           

          XHTML

          <h:panelGrid columns="3" >

            <rich:fileUpload id="upload" autoclear="true" fileUploadListener="#{fileUploadHome.listener}"

            maxFilesQuantity="#{fileUploadHome.uploadsAvailable}"

            immediateUpload="true" addControlLabel="Upload file" acceptedTypes="csv"

            allowFlash="false" listHeight="60px" noDuplicate="true">

           

           

            <a4j:support event="onclear" reRender="upload, infoFile, itemPanel" action="#{fileUploadHome.fileWrapper.clear}"/>

            <a4j:support event="onuploadcomplete" reRender="infoFile, itemPanel" action="#{itemHome.renderItemPanel}"

            actionListener="#{fileUploadHome.fileWrapper.setComplete(true)}"/>

            </rich:fileUpload>

           

            <rich:panel id="infoFile">

            <rich:dataTable value="#{itemHome.fileUpload}"

              var="file" id="fileRecordsTable">

            <h:column>

            <f:facet name="header">File Name</f:facet>

            <h:outputText value="#{file.fileName}"/>

            </h:column>

            </rich:dataTable>

            </rich:panel>

          </h:panelGrid>