1 Reply Latest reply on May 14, 2013 11:22 PM by sheriman xavier

    File Upload with richfaces

    sheriman xavier Newbie

      Hello,

       

      i working on file upload using richfaces + jsf, i follow some example from this site http://prabinhada.blogspot.com/2012/07/upload-file-using-jsf-20-and-richfaces.html,

      but still did no work, here my code

       

      FileUploadController.java

      import java.io.File; 

      import java.io.FileOutputStream; 

      import java.io.IOException; 

      import java.io.OutputStream; 

      import java.io.Serializable; 

      import java.util.ArrayList; 

       

      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.Scope;

      import org.jboss.seam.log.Log;

      import org.richfaces.event.FileUploadEvent; 

      import org.richfaces.model.UploadedFile; 

       

       

       

       

       

      @Name("fileUploadController")

      @Scope(ScopeType.SESSION)

        public class FileUploadController implements Serializable {

       

                /**

                 *

                 */

                private static final long serialVersionUID = -4900598368990838652L;

       

       

       

       

       

       

                @Logger

                private static Log log;

       

                          private String mime;

            private ArrayList<UploadedText> files = new ArrayList<UploadedText>();

                 

                 

             private static final String fPath = "C:\\upload\\"; 

            

            

             public void paint(OutputStream stream, Object object) throws IOException { 

                  stream.write(getFiles().get((Integer) object).getData()); 

                  stream.close(); 

             } 

             public void listener(FileUploadEvent event) throws Exception { 

                  UploadedFile item = event.getUploadedFile(); 

                  UploadedText file = new UploadedText(); 

                 file.setLength(item.getData().length); 

                  file.setName(item.getName()); 

                  file.setData(item.getData()); 

                  files.add(file); 

                  System.out.println("listener......");

                  System.out.println(item.getName());

                  System.out.println(files);

                  System.out.println(file);

                

             } 

             public String clearUploadData() { 

                  files.clear(); 

                  return null; 

             } 

             public int getSize() { 

                if (getFiles().size() > 0) { 

                             System.out.println("getSize");

                       return getFiles().size(); 

                 

                  } else { 

                            System.out.println("getsize return 0");

                       return 0; 

                  } 

             } 

             public long getTimeStamp() { 

                  return System.currentTimeMillis(); 

             } 

             public ArrayList<UploadedText> getFiles() { 

                  return files; 

             } 

             public void setFiles(ArrayList<UploadedText> files) { 

                  this.files = files; 

             } 

             public void writeFile() { 

                  FileOutputStream fop = null; 

                 File file; 

                  UploadedText contain = files.size() > 0 ? files.get(0) : null;

                  System.out.println("upload file.........");

                  System.out.println(contain);

                  if (contain == null) { 

                      return; 

                  } 

                  try { 

                      file = new File(fPath + contain.getName()); 

                       fop = new FileOutputStream(file); 

                       if (!file.exists()) { 

                            file.createNewFile(); 

                            System.out.println("file created");

                       } 

                       fop.write(contain.getData()); 

                       System.out.println(" fop.write(contain.getData()); ");

                      fop.flush(); 

                       fop.close(); 

                       if (log.isDebugEnabled()) { 

                           log.debug("File Creation Completed Successfuly."); 

                       } 

                  } catch (IOException e) { 

                       e.printStackTrace(); 

                  } finally { 

                       try { 

                            if (fop != null) { 

                                 fop.close(); 

                            } 

                       } catch (IOException e) { 

                           e.printStackTrace(); 

                       } 

                  } 

             }

                public String getMime() {

                          return mime;

                }

                public void setMime(String mime) {

                          System.out.println("setMime");

       

                          this.mime = mime;

                } 

            

            

        } 

       

       

      UploadText.java

       

      import java.io.Serializable; 

       

       

      import org.jboss.seam.ScopeType;

      import org.jboss.seam.annotations.Name;

      import org.jboss.seam.annotations.Scope;

       

       

      @Name("uploadText")

      @Scope(ScopeType.SESSION)

      public class UploadedText implements Serializable { 

             private static final long serialVersionUID = -3957467715082208719L; 

             private String name; 

            private String mime; 

             private long length; 

             private byte[] data; 

            public String getName() { 

                  return name; 

             } 

             public void setName(String name) { 

                  int extDot = name.lastIndexOf('.'); 

                  if (extDot > 0) { 

                       String extension = name.substring(extDot + 1); 

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

                            mime = "text/plain"; 

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

                            mime = "text/csv"; 

                      } else if("gif".equals(extension)){

                                mime = "image/gif";

                      }

                      

                       else { 

                            mime = "text/unknown"; 

                       } 

                  } 

                  this.name = name; 

             } 

             public String getMime() { 

                  return mime; 

             } 

             public void setMime(String mime) { 

                  this.mime = mime; 

             } 

             public long getLength() { 

                 return length; 

             } 

             public void setLength(long length) { 

                  this.length = length; 

             } 

             public byte[] getData() { 

                  return data; 

             } 

             public void setData(byte[] data) { 

                  this.data = data; 

             } 

        } 

       

      upload.xhtml

       

       

      <?xml version='1.0' encoding='UTF-8' ?> 

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

      <ui:composition xmlns="http://www.w3.org/1999/xhtml

             xmlns:f="http://java.sun.com/jsf/core

             xmlns:h="http://java.sun.com/jsf/html

             xmlns:ui="http://java.sun.com/jsf/facelets

             xmlns:a4j="http://richfaces.org/a4j

             xmlns:rich="http://richfaces.org/rich"> 

      <h:head></h:head>

       

      <f:view>

       

        <h:body> 

            <h:outputStylesheet>

              .top {

              vertical-align: top;

              }

       

              .info {

              height: 202px;

              overflow: auto;

              }

          </h:outputStylesheet>

                       <h:form enctype="multipart/form-data"> 

                            <h:panelGrid columns="2" columnClasses="top,top"> 

                                 <rich:fileUpload 

                                      id="upload" acceptedTypes="txt,csv,gif" 

                                      ontyperejected="alert('Only Text and CSV files are accepted');" 

                                      maxFilesQuantity="1"

                                     

                                    

                                      fileUploadListener="#{fileUploadController.listener}"> 

                                      <a4j:ajax event="uploadcomplete" execute="@this" render="info" />

                                      

                                 </rich:fileUpload> 

                               

                            </h:panelGrid> 

                            <h:panelGroup id="info"> 

                                 <rich:dataGrid columns="1" value="#{fileUploadController.files}" 

                                     var="file" rowKeyVar="row"> 

                                      <rich:panel bodyClass="rich-laguna-panel-no-header"> 

                                           <h:panelGrid columns="2"> 

                                                <a4j:mediaOutput element="img" mimeType="image/jpeg" 

                                                    createContent="#{fileUploadController.paint}" value="#{row}" 

                                                    style="width:100px; height:100px;" cacheable="false"> 

                                                     <f:param value="#{fileUploadController.timeStamp}" name="time" /> 

                                                </a4j:mediaOutput> 

                                                <h:panelGrid columns="2"> 

                                                     <h:outputText value="File Name:" /> 

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

                                                     <h:outputText value="File Length(bytes):" /> 

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

                                                </h:panelGrid> 

                                           </h:panelGrid> 

                                     </rich:panel> 

                                 </rich:dataGrid> 

                                 <a4j:commandButton action="#{fileUploadController.clearUploadData}" 

                                      render="info, upload" value="Clear Uploaded Data" 

                                      rendered="#{fileUploadController.size>0}" /> 

                                 <a4j:commandButton action="#{fileUploadController.writeFile}" 

                                      value="Write file to Disk" rendered="#{fileUploadController.size>0}"/> 

                            </h:panelGroup> 

                       </h:form> 

       

       

      </h:body>

      </f:view>

      </ui:composition>

       

      component.xml

       

      <web:multipart-filter create-temp-files="true"

                            max-request-size="1024000" url-pattern="*.seam"/>

       

       

      i want to upload the file inside this directory

       

         private static final String fPath = "C:\\upload\\"; 

       

      but i not working, can anyone tell me where i did wrong, because it not showing any error or exception on command prompt

       

      Thanks