【发布时间】:2012-02-28 13:27:41
【问题描述】:
我知道如何使用 Primefaces 或 Tomahawk 进行文件上传,但是,我正在尝试使用 Apache Commons FileUpload 进行文件上传,到目前为止我遇到了一些障碍。即使我的表单使用multipart/form-data,当我提交表单时,内容类型也会变成application/x-www-form-urlencoded。这是我的代码
<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>
这是我的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();
}
}
}
所以当我尝试打印上面的内容类型时,它显示了application/x-www-form-urlencoded。如果我把ajax="false"放到我的p:commandButton中,那么submit()方法甚至不会被调用,但是如果我取出enctype="multipart/form-data"(仍然保留ajax="false"),那么submit()被调用但它不是多部分的,它是application/x-www-form-urlencoded,因此 apache commons fileupload 抛出异常,因为它不是多部分的。似乎无论我做什么,我似乎都无法获得多部分请求。请帮忙
【问题讨论】:
标签: jsf file-upload jsf-2 apache-commons-fileupload