The upload is stored in a bean property of type referenced in the value attribute. h:inputFile is basically used like any other JSF input component. If you're using at least Servlet 3.0 (Tomcat 7, Jetty 9, JBoss AS 6, GlassFish 3, etc), then you can just use standard API provided HttpServletRequest#getPart() to collect the individual multipart form data items (most Servlet 3.0 implementations actually use Apache Commons FileUpload under the covers for this!). As JSF 2.2 requires Servlet 3.0 it is finally possible to have the file upload component h:inputFile in the standard. When you're already on Servlet 3.0 or newer, use native API Such a library has proven its robustness. You should rather use a real library which is used (and implicitly tested!) by millions of users for years. Many online sources have failed hard in this, such as.
You shouldn't try to do this on your own or copypaste some homegrown library-less code found elsewhere on the Internet. However, this is a precise and tedious work which requires precise knowledge of RFC2388. You can in theory parse the request body yourself based on ServletRequest#getInputStream(). This is where the well known Apache Commons FileUpload came into the picture. In the example below select the Choose File button. The component supports limitations on location selection, file size and provides file validation functionality to the application.
Īfter submitting such a form, the binary multipart form data is available in the request body in a different format than when the enctype isn't set.īefore Servlet 3.0, the Servlet API didn't natively support multipart/form-data. As stated in the HTML specification you have to use the POST method and the enctype attribute of the form has to be set to "multipart/form-data". To browse and select a file for upload you need a HTML field in the form.