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
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
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.
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
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);
// ...