I want to upload files larger than 2 GB with Java EE with the use of primefaces. In general that it's not that big deal, but I have a little problem. The usually way with PrimeFaces is to upload a file, have that UploadedFile-Object, obtain the InputStream from that and then write it to disk.
But writing to disk large files is a time intensive task. Instead I would like to find the uploaded file in the file system and then move it to the folder, where I store all the files, because moving files is not wasting time.
So, according to this question here I figured out to set PrimeFaces-Tmp-folder and the size-limit, when files will put there. Now every uploaded file just goes directly into that folder. The point is, that I now have a file on disk, while the user is uploading - and not creating it afterwards.
So far, so good. I could identify the file and just move it (although it has a strange name). But pointing to Primefaces Userguide (Page 187), this Tmp-Folder is just used internally. And I even would steal the contents of the UploadedFile-Object from Primefaces. Seems not to be a clean solution to me.
Any hints how to achieve this ?
I also saw this question. Could also be my title, but it's not what I am looking for.
EDIT:
Because of the comment, some code. First I am collecting the UploadFile-Objects in the fileUploadListener:
public void handleFileUpload(FileUploadEvent event) throws IOException {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
getUploadedFilesDTO().add(new UploadedFileDTO(event.getFile(), uploadedFilesCounter));
uploadedFilesCounter++;
}
Second in the EJB I am calling copyFile for every UploadedFile-Object:
private void copyFile(UploadedFile uploadedFile, String randomFilename) throws IOException {
InputStream originalFile = uploadedFile.getInputstream();
OutputStream fileOutput = null;
try {
fileOutput = new FileOutputStream("/pvtfiles/" + randomFilename);
IOUtils.copy(originalFile, fileOutput);
} finally {
IOUtils.closeQuietly(fileOutput);
IOUtils.closeQuietly(originalFile);
}
}
By creating your own filter It will not create temp files. Please read my answer below on another post
https://stackoverflow.com/a/19581257/185022
I was able to do this by writing a custom filter, as AZ_ suggested, and adding the following:
The trick is to usethe
MultipartRequest
object to get the aFileItem
object.FileItem
'swrite
method can be used to save the file in a different location or using a different name. According to the documentation, some implementations may still do this with a copy, but what I am using (Apache Commons 1.3) seems to do this with a rename/move as desired. Note that the string used ingetFileItem
to find theFileItem
is the ID attribute from the<p:fileUpload>
tag.More Code:
Modified portion of
web.xml
:Sample jsf:
Sample custom filter class: