0

我正在使用 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());
                }
    }
}

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

4

1 回答 1

0

尝试添加enctype="multipart/form-data"到您的表单标签,就像在这个例子中一样

于 2013-02-28T08:42:42.110 回答