Fileupload fileUploadListener仅在每按一次按钮时调用

时间:2015-05-29 08:52:32

标签: jsf file-upload primefaces

我试图在primefaces 5.2中使用fileupload组件上传文件,但我发现fileUploadListener的函数bind并不是每次都被调用。我第一次按下上传按钮时会调用它,但不会在第二次调用。我第三次按下上传按钮时会调用它,但不会在第四次调用。以下是我的代码和设置。

Web.xml中

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>auto</param-value>
</context-param><filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
    org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

XHTML

<composite:implementation>
        <p:fieldset legend="#{cc.attrs.tittle}" id="transfile" styleClass="fieldset" style="width:60%">
        <h:form enctype="multipart/form-data">
            <p:fileUpload fileUploadListener="#{transferFRorzwBean.translate}"
                dragDropSupport="false" mode="advanced" sizeLimit="100000"
                allowTypes="/(\.|\/)(txt|csv)$/" immediate="true"
                label="Open" uploadLabel="Translate"/>
        </h:form>
        </p:fieldset>
</composite:implementation>

Java代码

@ManagedBean
@ViewScoped
public class TransferFRorzwBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private UploadedFile file;

public void translate(FileUploadEvent event) {
    this.file = event.getFile();
    String filename = FilenameUtils.getName(file.getFileName());
    uploadFileName = filename;
    InputStream input = file.getInputstream();
    String path = FacesContext.getCurrentInstance()
            .getExternalContext().getRealPath("/Protected/upload");
    OutputStream output = new FileOutputStream(new File(path, filename));

    try {
        IOUtils.copy(input, output);
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }
}
public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

IDE:Eclipse Kepler Service Release 1 服务器:Tomcat 8.0 Jar:commons-fileupload-1.3.1.jar,commons-io-2.4.jar,primefaces-5.2.jar 我也尝试在web.xml上添加以下内容,但这不起作用......

<filter-mapping>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>
   <dispatcher>FORWARD</dispatcher>
</filter-mapping>

任何人都有这个问题的想法吗?

1 个答案:

答案 0 :(得分:0)

我通过在java中添加代码解决了我的问题,如下所示:

event.getComponent().setTransient(false);