Primefaces FileUpload不适用于Spring

时间:2013-02-28 08:27:48

标签: java spring primefaces

我正在使用Spring 3.0.6.RELEASE和Primefaces 2.2.1做一个Web应用程序。在我的应用程序中有一个FileUpload组件。但是fileupload不工作。在backingbean FileUploadController中的upload()函数是从未打过电话。

我将这个包含在我的web.xml中:

<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>

在我的index.xhtml

<p:dialog widgetVar="assetUploadDialog" modal="true" minHeight="300"
                  minWidth="550" closable="true" draggable="true"
                  closeOnEscape="true" header="Asset upload">
             <h:form id="formuploader" >
                <p:fileUpload id="fileuploader"
                      fileUploadListener="#{fileUploadController.upload}"
                      sizeLimit="100000"
                      allowTypes="*.jpg;*.gif;*.png;*.pdf;*.png;*.doc?x;*.xls;*.xlsx;"/>
            </h:form>
        </p:dialog>

在我的FileUploadController梁类中:

public class FileUploadController {

    private String destination="D:\\";

     public void upload(FileUploadEvent event) {  
        System.out.println("uplaod");
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);

        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }  
     public void copyFile(String fileName, InputStream in) {
           try {

                OutputStream out = new FileOutputStream(new File(destination + fileName));

                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }

                in.close();
                out.flush();
                out.close();

                System.out.println("New file created!");
                } catch (IOException e) {
                System.out.println(e.getMessage());
                }
    }
}

我的问题的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

尝试将enctype="multipart/form-data"添加到您的表单标记中,就像在此example

中一样