4 Replies Latest reply on Jul 11, 2011 4:59 AM by nicolettait

    upload file into web directory

    alejandropv
      Hi, I need to upload images files, I try <s:fileUpload> component, and it works fine for me, but every example I find, saves the file to database and I need to do it into a web directory. Is this possible? Can somebody show me an example?

      thanks in advance
        • 1. Re: upload file into web directory
          cripto

          Excuse me, could perform what you
          Wait?
          If so, you could put your SOLUTION, PLEASE?
          Is that I am having the same problem


          Thank you

          • 2. Re: upload file into web directory
            michaelcourcy

            This work for me


            @Name("uploadHandler")
            @Scope(ScopeType.EVENT)
            public class UploadHandler {
                      
                 @In(value="#{facesContext}")
                 FacesContext facesContext;
                 
                 @Logger private Log log;
                 
                 
                 private byte[] file;      
                 
                 public void handleUpload() throws IOException{
                      
                      String path = ((ServletContext) facesContext.getExternalContext().getContext()).getRealPath("/my/files/dir");
                      File f = new File(path,"myFile.txt");
                      log.info(f);
                      FileOutputStream fo = new FileOutputStream(f);
                      fo.write(file);
                      fo.flush();
                      fo.close();
                 
                 }
            
                 public byte[] getFile() {
                      return file;
                 }
            
                 public void setFile(byte[] file) {
                      this.file = file;
                 }
            
            }
            



            and the form


            <h:form id="fooForm" enctype="multipart/form-data">
                    
                       <s:fileUpload id="file" 
                                data="#{uploadHandler.file}"
                                accept="image/jpg"
                          />
                    
                        <h:commandButton id="upload" value="Upload !!" 
                                         action="#{uploadHandler.handleUpload}"/>                      
                    
                    </h:form>
            



            But I don't recommand to use the  ....getRealPath(/my/files/dir);.... instead use a path that you configure in seam.properties.

            • 3. Re: upload file into web directory
              raman
              • 4. Re: upload file into web directory
                nicolettait

                Hi! I've tried the code suggested by Michael, same .java, same .xhtml, adding to the components.xml the following configuration




                <web:multipart-filter create-temp-files="true"
                        max-request-size="1000000"
                        url-pattern="*.seam"/>





                The file it's always uploaded correctly, but it throws a null pointer exception on the line
                fo.write(file);


                Maybe it'a a matter of file system permission, but I haven't found a good solution to this problem, has anyone experienced the same problem?