I know how to do file upload using Primefaces or using Tomahawk, however, I am trying to doing file upload using Apache Commons FileUpload and so far I am having a bit of road block. Even though my form use multipart/form-data
, when I submit my form, the content type become application/x-www-form-urlencoded
. Here is my code
<h:body>
<h:form enctype="multipart/form-data">
Upload File
<input type="file" name="file"/>
<p:commandButton value="Submit" action="#{viewBean.submit}"/>
</h:form>
</h:body>
Here is my ViewBean
@ManagedBean
@ViewScoped
public class ViewBean implements Serializable {
public void submit() {
String url = "/FileUploadServlet";
FacesContext context = FacesContext.getCurrentInstance();
try {
String contentType = context.getExternalContext().getRequestContentType();
context.getExternalContext().dispatch(url);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception when calling Servlet", e);
} finally {
context.responseComplete();
}
}
}
So when I try to print the content type above, it showed application/x-www-form-urlencoded
. If I put ajax="false"
to my p:commandButton
, then the submit()
method is not even invoked, but if I take out enctype="multipart/form-data"
(still keep ajax="false"
), then submit()
is invoked but it is not multipart, it is application/x-www-form-urlencoded
, so apache commons fileupload throw an exception since it is not multipart. Seems like whatever I do, I cant seems to get the multipart requrest. Please help