如何让JSF上传Apache Commons FileUpload文件 [英] How to get JSF to upload file with Apache Commons FileUpload

查看:203
本文介绍了如何让JSF上传Apache Commons FileUpload文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用Primefaces或者使用Tomahawk做文件上传,但是我正在使用Apache Commons FileUpload来做文件上传,到目前为止我还是有一些路障。即使我的表单使用 multipart / form-data ,当我提交表单时,内容类型变成 application / x-www-form-urlencoded 。这是我的代码

 < h:body> 
< h:form enctype =multipart / form-data>
上传文件
< input type =filename =file/>
< p:commandButton value =Submitaction =#{viewBean.submit}/>
< / h:表格>
< / h:body>

这是我的 ViewBean



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ @ $ b String url =/ FileUploadServlet;
FacesContext context = FacesContext.getCurrentInstance();
try {
String contentType = context.getExternalContext()。getRequestContentType();
context.getExternalContext()。dispatch(url);
} catch(Exception e){
logger.log(Level.SEVERE,调用Servlet时出现异常,e);
} finally {
context.responseComplete();





$ p $所以当我尝试打印上面的内容类型,它显示了 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会抛出一个异常,因为它不是multipart。看起来像我所做的,我似乎不能得到多部分的要求。请帮忙解决方案所以,当我尝试打印上面的内容类型时,它显示/ b>

< p:commandButton> 默认发送1级的ajax请求 XMLHttpRequest 。这是不是支持 multipart / form-data 。只有级别2 XMLHttpRequest 支持它只支持在最新的浏览器(那些也支持HTML5),并没有在JSF JS API或PrimeFaces JS API中实现。







如果我把ajax =false放到我的p:commandButton中,那么submit()方法甚至不会被调用


这种方式可以发送完整的 multipart / form-data 。没有调用submit方法只是因为2.2版之前的JSF不支持 multipart / form-data 请求。 JSF通过基础HTTP servlet请求中的 request.getParameter() getParameterMap()来收集提交的数据。但是,当使用 application / x-www-form-urlencoded 以外的编码时,这将返回 null 。当JSF根据提交的数据确定要调用的操作方法时,当数据是 null 时,它将无法找到并调用它。



理论上,如果您创建了一个使用Apache Commons FileUpload或者新的Servlet 3.0的 Filter request.getPart( ) / getParts() 方法从 multipart / form-data 请求并用一个自定义的实现封装当前的HTTP servlet请求,这个自定义的实现覆盖了提供数据映射的 getParameter()调用,那么JSF将能够根据 getParameter()调用的结果完成所需的工作。你可以在这篇文章和稍微改变以利用Apache Commons FileUpload在

即将推出的JSF 2.2将有一个新的< h:inputFile> code>可绑定到Servlet 3.0的组件 部分 属性

 < h:form ENCTYPE = 多部分/格式数据 > 
< h:inputFile value =#{bean.file}/>
< h:commandButton value =submitaction =#{bean.submit}/>
< / h:表格>



 私人部分文件; JSF 2.2最终版本计划在Q1的后期发布,但目前可用作快照版本。  

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

解决方案

So when I try to print the content type above, it showed application/x-www-form-urlencoded.

The <p:commandButton> sends by default an ajax request of level 1 XMLHttpRequest. This does not support multipart/form-data. Only level 2 XMLHttpRequest supports it, but it's only supported in the newest browsers (those also supporting HTML5) and not implemented in JSF JS API nor in PrimeFaces JS API.


If I put ajax="false" to my p:commandButton, then the submit() method is not even invoked

This way however a fullworthy multipart/form-data will be sent. That the submit method is not invoked is just because JSF prior to version 2.2 does not support multipart/form-data requests out the box. JSF collects the submitted data by default using request.getParameter() and getParameterMap() on the underlying HTTP servlet request. However, this will return null when an encoding other than application/x-www-form-urlencoded is been used. As JSF determines the to-be-invoked action method based on submitted data, it won't be able to locate and invoke it when the data is null.

In theory, if you create a Filter which uses Apache Commons FileUpload or the new Servlet 3.0 request.getPart()/getParts() methods to extract the data from the multipart/form-data request and wraps the current HTTP servlet request with a custom implementation which overrides the getParameter() calls wherein a mapping of the extracted data is been supplied, then JSF will be able to do the needed job based on results of getParameter() calls. You can find a concrete example utilizing the Servlet 3.0 API in this article and the same example which is slightly changed to utilize Apache Commons FileUpload in this answer.

The upcoming JSF 2.2 will have a new <h:inputFile> component which is bindable to a Servlet 3.0 Part property.

<h:form enctype="multipart/form-data">
    <h:inputFile value="#{bean.file}" />
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

with

private Part file;

JSF 2.2 final release is scheduled for late end of Q1, but is currently available as a snapshot release.

这篇关于如何让JSF上传Apache Commons FileUpload文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆