I want to upload files larger than 2 GB using Java EE using simple elements. In general, this is not so important, but I have a little problem. Usually with PrimeFaces you can upload a file, load this UploadedFile-Object, get an InputStream from it and write it to disk.
But writing large files to disk is a time-consuming task. Instead, I would like to find the downloaded file in the file system, and then move it to the folder where I store all the files, because moving files does not waste time.
So, in accordance with this question here, I decided to set the PrimeFaces-Tmp folder and the size-limit when the files will be placed there. Now each downloaded file goes directly to this folder. The fact is that I now have a file on disk, and the user downloads - and does not create it afterwards.
So far so good. I could identify the file and just move it (although it has a strange name). But, pointing to the Primefaces Userguide (Page 187) , this Tmp folder is used only internally. And I would even steal the contents of the UploadedFile-Object from Primefaces. This does not seem to be a clean solution for me.
Any clues on how to do this?
I also saw this question . It could be my name, but thatโs not what Iโm looking for.
EDIT:
Due to the comment, some code. First, I collect the UploadFile objects in 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 EJB, I call copyFile for each 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); } }