1

我想实现primefaces File uploader

我的实现:

<h:form enctype="multipart/form-data">
                                    <p:fileUpload id="fileUpload"
                                        fileUploadListener="#{fileUploadController.handleFileUpload}"
                                        mode="advanced" update="messages" sizeLimit="100000"
                                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

                                    <p:growl id="messages" showDetail="true" />

我在我的 web.xml 文件中配置了我的过滤器:

<!-- ############################################# -->
    <!-- # File upload                                      # -->
    <!-- ############################################# -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>
            org.primefaces.webapp.filter.FileUploadFilter
        </filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>6400</param-value>
        </init-param>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>/resources/uploads</param-value>
        </init-param>
    </filter>

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

在我的 pom.xml 中:

  • 公共文件上传
  • commons-io

我的文件上传控制器看起来像这样:

@Component
@Scope("session")
public class FileUploadController {

    private int size = 1024;

    /**
     * log4j
     */
    private static Logger log = LogManager.getLogger(FileUploadController.class);

    public void handleFileUpload(FileUploadEvent event) {  
        log.info("Method handleFileUpload invoked");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        try {
            File targetFolder = new File("/resources/uploads/images");
            InputStream inputStream = event.getFile().getInputstream();
            OutputStream out = new FileOutputStream(new File(targetFolder,
                    event.getFile().getFileName()));
            int read = 0;
            byte[] bytes = new byte[size];
            log.info("read file stream");
            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error(e);
            e.printStackTrace();
        }

    }

我的问题是,我没有从控制器获得日志消息来上传我的文件?

真的很感谢你的回答!!!

顺便说一句,我的 PF 版本是 3.5

更新

我的模板看起来像这样:

</h:head>
<h:body>
    <div class="well" style="margin-top: 50px">
        <div class="container">

            <h:form>
                <p:growl id="growl" sticky="true" showDetail="true" />

                <p:wizard widgetVar="wiz"
                    flowListener="#{productWizardHandler.onFlowProcess}">

                    <!-- Basic Product Tab -->
                    <p:tab id="basicProduct" title="BasicProduct">

                        <p:panel header="Fill in your basic Product Information">

                            <h:messages errorClass="error" />
                            <h:panelGrid columns="2" columnClasses="label, value"
                                styleClass="grid">
                                <h:outputText value="Product Name: *" />
                                <p:inputText required="true" label="Product Name"
                                    value="#{productService.instance.productName}" />

                                <h:outputText value="Categorie: *" />
                                <p:inputText label="Categorie" value="#" />

                                <h:outputText value="Description: *" />
                                <p:inputTextarea required="true"
                                    value="#{productService.instance.description}" />

<!-- Select your product  -->
<!--                                <h:outputLabel value="Select the product you want to create:" for="dd" />
                                <p:autoComplete id="dd" dropdown="true"
                                    value="#{autoCompleteBean.txt6}"
                                    completeMethod="#{autoCompleteBean.complete}" />
 -->
                            </h:panelGrid>

                        </p:panel>
                    </p:tab>

                    <!-- Image Tab -->
                    <p:tab id="image" title="Image">
                        <p:panel header="Upload your Product Image Details">

                            <h:messages errorClass="error" />

                            <h:panelGrid columns="2" columnClasses="label, value"
                                styleClass="grid">
                                <h:form enctype="multipart/form-data">

                                    <p:fileUpload id="fileUpload"
                                        fileUploadListener="#{fileUploadController.handleFileUpload}"
                                        mode="advanced" update="messages" sizeLimit="100000"
                                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

                                    <p:growl id="messages" showDetail="true" />

                                </h:form>
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>

                    <!-- Special Tab -->

                    <p:tab id="speziel" title="Spezial Tab">

                        <!-- ################################################################################ -->
                        <p:panel>
                            <ui:include src="product_templates/test.xhtml" />
                            <ui:include src="product_templates/clothes.xhtml" />
                            <ui:include src="product_templates/arbnb.xhtml" />

                        </p:panel>
                    </p:tab>

                    <!-- Summary -->
                    <p:tab id="summary" title="Summary">
                        <p:panel header="Product Summary">

                            <h:panelGrid id="productSummary" columns="6">
                                <h:outputText value="Product Name: " />
                                <h:outputText styleClass="outputLabel"
                                    value="#{productWizardHandler.product.productName}" />

                                <h:outputText value="Categorie: " />
                                <h:outputText styleClass="outputLabel" value="#" />

                                <h:outputText value="Description: " />
                                <h:outputText styleClass="outputLabel"
                                    value="#{productWizardHandler.product.description}" />>  

                      INSERT outputs für speziell products
                                                </h:panelGrid>

                            <p:commandButton value="Submit" update="growl"
                                actionListener="#{productWizardHandler.save}" />

                        </p:panel>
                    </p:tab>

                </p:wizard>

            </h:form>
        </div>
    </div>
</h:body>

在此处输入图像描述

4

1 回答 1

0

我认为您没有为 primefaces 文件上传过滤器指定过滤器映射

  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>
于 2013-04-16T07:42:01.847 回答