If we are using Java Server Faces (JSF), we have the page with some fields and file upload menu the its add more complexity, fields are binded to backing bean but these file uploads components are need to tie up with some 3rd party file upload libraries.
In Primefaces provide easy way to do the file upload in JSF web application, even though primefaces internally used the same Apache Commons FileUpload Libraries, but provide simple JSF tags. We need configure some listeners.
In Servlet 3.0 they introduced the Part Interfaces and @MultiPartConfig annotations for easy way to do the file upload without any 3rd party libs, Servlet 3.0 is part of Java EE 6 specification. so any servlet 3.0 implementation servers like Tomcat 7, Jboss 6, GlassFish 3 they can get benefit of servlet 3.0. but this feature is not available in JSF 2.1. so again still it's complex to use file upload in JSF, because we need to mix servlet and JSF backing bean.
At last JSF 2.2 is added the File Upload component, so we just use <h:inputFile> tag to make the file upload web application so easily, and this component use the Part interface, which its introduced its part of Servlet 3.0. so this JSF2.2 libraries is working perfectly with Tomcat 7 (because tomcat 7 is implemented the Servlet 3.0 spec).
To bring the File Upload feature we need to follow 3 step process
- Add <h:inputFile> tag in JSF page
- Add enctype="multipart/form-data" attribute in enclosing <h:form> tag
- Create the field in Backing bean with Part data type.
Setup the EnvironmentAdd JavaServerFace framework in Netbeans and choose JSF2.2 library
Step 1: Add <h:inputFile> tag in JSF page
In JSF page add the <h:inputFile> tag where we need file upload component
here we bind the value property to file1 field, which data type is Part, check the third step
Step 2: Add enctype="multipart/form-data" attribute in enclosing <h:form> tag
When u want file upload feature then we need to inform the browser that form may contain multiple normal text input field and some file upload component, so prepare the POST request in special form, like parts by part with special delimiter boundary.
so index.xhtml file looks like
Step 2: Create Backing Bean
now create the backing bean for this page. we need to create the fields for input File component with Part type. Part interface is introduced in servlet 3.0 for represent the file chunks sent by browser, JSF 2.2 use this Part interface, Using the Part interface we can get the header information of each file chunk sent by browser, these header contain the file name and other meta information about the file.
Part interface also contain write() method to write the file contents into specified server location, or u can get the InputStream then we can process it.
here we need get the file name from the Header of the chunk, so there is boilerplate code can do, here i don't like this method :-(
there is no direct method to retrieve the file name. so use this code we can get the name of the file is uploaded by client, then using write method we can store into server.
Get the sample project from Github or download from here