The PrimeFaces File upload component can be used to perform server side file uploads. Let's see in this quick tutorial how to achieve it in some simple steps.
The first thing you need is adding some libraries to your application. As a matter of fact, PrimeFaces file upload relies on Apache commons-file-upload and commons-io libraries. So dowload them and add them to your WEB-INF/lib path:
Here you can download Apache Commons io
Here you can download Apache Commons File Upload
As far as web.xml configuration is concerned you need to declare the PrimeFaces filter in order to enable file uploading
Good, your configuration is completed. Let's see a concrete example:
The tag does the job of sending the multipart stream to the server. In the bean declared (fileUploadController) a listener will fire which will let you handle the file and do what you want with it.
Here's the bean implementation:
The upload method provides as parameter the FileUploadEvent from which you can retrieve the temporary file uploaded on the server. Once you have got it, use the stream to copy it or add it to a relational database.
As additional note, you can choose the path where the file is uploaded on the server by setting some additional parameters like uploadDirectory and thresholdSize
thresholdSize specifies the maximum file size in bytes to keep uploaded files in memory. If it
exceeds this limit, it’ll be temporarily written to disk.
uploadDirectory is the folder where to keep temporary files that exceed thresholdSize.