Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the p:fileUpload uploaded file saved and how do I change it?

I use the simple File upload of Primefaces in development with Netbeans. My test example is similar to to the Primefaces manual.
My question: where does the file get uploaded on my local computer? How can I change the path for it? Thx!

The jsf file:

<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Page test</title>
    </h:head>
    <h:body>
        Hello! My first JSF generated page!
        <h:form enctype="multipart/form-data">
            <p:fileUpload value="#{fileBean.file}" mode="simple" />
            <p:commandButton value="Submit" ajax="false"/>
        </h:form>

    </h:body>
</html>

and the managed bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;


    @ManagedBean

@RequestScoped
public class FileBean {

    private UploadedFile file;

    public FileBean() {
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;

    }
}
like image 611
seinecle Avatar asked Aug 06 '12 14:08

seinecle


1 Answers

It's by default saved in either the servlet container's memory or the temp folder, depending on the file size and the Apache Commons FileUpload configuration (see also "Filter Configuration" section of the <p:fileUpload> chapter in PrimeFaces User's Guide).

You shouldn't worry about this at all. The servlet container and PrimeFaces know exactly what they do. You should in the command button's action method actually be saving the uploaded file contents to the location where you need it to be. You can get the uploaded file contents as an InputStream by UploadedFile#getInputStream() or as a byte[] by UploadedFile#getContents() (getting a byte[] is potentially memory expensive in case of large files, you know, each byte eats one byte of JVM's memory, so don't do that in case of large files).

E.g.

<p:commandButton value="Submit" action="#{fileBean.save}" ajax="false"/>

with

private UploadedFile uploadedFile;

public void save() throws IOException {
    String filename = FilenameUtils.getName(uploadedFile.getFileName());
    InputStream input = uploadedFile.getInputStream();
    OutputStream output = new FileOutputStream(new File("/path/to/uploads", filename));

    try {
        IOUtils.copy(input, output);
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }
}

(FilenameUtils and IOUtils are from Commons IO which you should anyway already have installed in order to get <p:fileUpload> to work)

To generate unique file names, you may find File#createTempFile() facility helpful.

String filename = FilenameUtils.getName(uploadedFile.getFileName());
String basename = FilenameUtils.getBaseName(filename) + "_";
String extension = "." + FilenameUtils.getExtension(filename);
File file = File.createTempFile(basename, extension, "/path/to/uploads");
FileOutputStream output = new FileOutputStream(file);
// ...
like image 80
BalusC Avatar answered Sep 22 '22 19:09

BalusC