<p:fileupload> not working

UI Components for JSF
Post Reply
User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

29 Jun 2011, 15:58

Hi there,

I'm using the freshest snapshot directly from SVN.
Glassfish 3.1
Netbeans under Win7.

The bean doesn't get called.
I tried it with the <p:outputPanel> and without,
I also tried prependID="false", hoping it would help.
I also tried the filters in web.xml

No way. Please help !

Regards,
Holger

Here is the code :

Code: Select all

    <h:body>
        <h:form id="uploadformid" enctype="multipart/form-data">
            <p:outputPanel id="uploadpanel">
                <p:fileUpload id="fuploadid" value="#{fileUploadController.file}" mode="simple" allowTypes=".jpg,.png,.jpeg,.bmp,.gif" />
                <p:commandButton id="uploadbuttonid" value="Submit" ajax="false" action="#{fileUploadController.upload1}"/> 
            </p:outputPanel>
        </h:form>
    </h:body>
The sessionbean :

Code: Select all

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.UploadedFile;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author awahe
 */
@ManagedBean(name="fileUploadController")
@SessionScoped
public class FileUploadController implements Serializable {

    private static final Logger logger = Logger.getLogger(FileUploadController.class.getName());
    private UploadedFile file;
    private String filename ="c:\\desert.jpg";

    public FileUploadController() {
        logger.info("------- FileUploadController : constructor");
    }

    public UploadedFile getFile() {
        logger.info("------- FileUploadController : getFile");
        return file;
    }

    public void setFile(UploadedFile file) {
        logger.info("------- FileUploadController : setFile");
        this.file = file;
    }

    public void handleFileUpload(FileUploadEvent event) {
        try {
            logger.entering(FileUploadController.class.getName(), "handleFileUpload");
            String localfilepath = event.getFile().getFileName();
            String remotefilepath = "/anon_ftp/pub/";
            logger.log(Level.INFO, "------- FileUploadController : upload : {0}", localfilepath);
            logger.exiting(FileUploadController.class.getName(), "handleFileUpload");
        } catch (Exception e) {
            logger.log(Level.SEVERE, "******* FileUploadController : handleFileUpload{0}", e.getMessage());
        }
    }

    public void upload() {
        String localfilepath = file.getFileName();
        String remotefilepath = "/anon_ftp/pub/";
        logger.log(Level.INFO, "------- FileUploadController : upload : {0}", localfilepath);
    }

    public void upload1() {
        String remotefilepath = "/anon_ftp/pub/";
        logger.log(Level.INFO, "------- FileUploadController : upload1 : {0}", getFilename());
    }

    /**
     * @return the filename
     */
    public String getFilename() {
        return filename;
    }

    /**
     * @param filename the filename to set
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }
}
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

29 Jun 2011, 17:59

Hi again,

perhaps a little little hint ?
Is this M2 related or am I too stupid ?

regards,
Holger
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 10:09

Hi again,

could it be related to the fact, that I'm including the latest apache commons lib ?

I think I read somewhere, that fileupload is using the commons lib.

I'm getting mad........
Regards,
Holger
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 11:10

Just tried it without apache commons in a completely fresh webapp.
No way. It's not working.
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 11:26

That's not the answer that I expected, LOL
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 16:14

OK,
after spending a lot of time I figured it out :

1.) include org.apache.commons.io from http://commons.apache.org/io/download_io.cgi
2.) include org.apache.commons.fileupload http://commons.apache.org/fileupload/do ... upload.cgi
as library
3.) add

Code: Select all

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>c:/test</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
to your web.xml
4.) Make sure your server has write access to c:/test

Now you get the uploadxyz.tmp files in c:\test

But, how do I determine the name of the uploadxyz.tmp file ?
This would be great to rename it or do operations with it !

Regards,
Holger
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 17:04

Hi,
I managed to write the contents from the uploadedFile to another file :

Code: Select all

            
File theNewFile = new File(uploadedFile.getFileName());
writeToFileByByte(uploadedFile.getContents(), theNewFile);
and the method to do so :

Code: Select all

    public void writeToFileByByte(byte[] data, File file) {
        try {
            
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
            out.write(data);
            out.close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "******* FileUploadController : writeToFileByByte : {0}", e.getMessage());
        }
    }
But again,
I have to delete the tempfile.

Regards,
Holger

PS: Is it possible to delete this b*i*t*c*h from the thread ? ;) She drives me crazy ;)
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

30 Jun 2011, 17:14

Code: Select all

public void handleFileUpload(FileUploadEvent event) {
    UploadedFile file = event.getFile();
    BufferedInputStream inputStream = new BufferedInputStream(file.getInputstream());
    /* your code */
}
The temp-files are deleted automatically after some time

User avatar
ITReppert
Posts: 57
Joined: 07 Feb 2011, 19:43

30 Jun 2011, 20:16

Hi,

thanks for the Stream method, I tried it too and it seems like using the byte is faster.

Regards,
Holger

Code: Select all

String theNewFileName = "c:" + File.separatorChar + "test" + File.separatorChar + file.getFileName();
            File theNewCheckFile = new File(theNewFileName);
            boolean theNewFileExists = (theNewCheckFile.exists());

            if (theNewFileExists) {
                logger.log(Level.INFO, "------- FileUploadController : upload1 : fileexists : {0} deleting : ", new Object[]{Boolean.toString(theNewCheckFile.delete()), theNewCheckFile.getAbsolutePath()});
            }
            theNewCheckFile = null;
            File theNewFile = new File(theNewFileName);

            logger.log(Level.INFO, "------- FileUploadController : upload1 : filepath : {0} : {1}", new Object[]{theNewFile.getAbsolutePath(), theNewFile.getCanonicalPath()});

//            writeToFileByInputStream(getFile().getInputstream(), theNewFile);
            writeToFileByByte(getFile().getContents(), theNewFile);

Code: Select all

    public void writeToFileByInputStream(InputStream is, File file) {
        try {

            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
            byte buf[] = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            is.close();
            out.close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "******* FileUploadController : writeToFileByInputStream : {0}", e.getMessage());
        }
    }
// Netbeans 7.2 // Glassfish 3.1.2 // Windows 7 (64 bit) // FireFox // PrimeFaces 3.4.2

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 46 guests