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
I used simple mode, GlassFish 4.0 and PrimeFaces 4.0
Backing Bean
private UploadedFile file;
private String destination="C:\\temp\\";
public void upload() {
System.out.println("uploading");
if(file != null) {
System.out.println("the file is" +file);
FacesMessage msg = new FacesMessage("Succesful" + file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
try {
copyFile(file.getFileName(), file.getInputstream());
}
catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("uploaf finished");
}
public void copyFile(String fileName, InputStream in) {
try {
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(destination + fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
System.out.println("New file created!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
JSF page
<p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/>
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);
// ...