0

我正在尝试将文本文档上传到我的服务器,这让我疯狂了几天。基本上,在文件上传时假设被触发的方法#{bookController.handleFileUpload} 不会触发,但我没有收到任何错误,并且在前端文件看起来像是已上传。我假设文件在到达我的控制器方法的途中被消耗掉了。这是我的 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>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>south-street</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/jpmorgan/*</url-pattern>
  </servlet-mapping>
  <context-param>

我在我的 pom.xml 中添加了必要的 jar(common-io.jar 和 common-fileupload.jar)。这是我假设处理文件的页面:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/layouts/no-ajax.xhtml">

    <ui:define name="head">

    </ui:define>

    <ui:define name="title">#{msg['add_book']}</ui:define>

    <ui:define name="content">
        <h:form id="form" enctype="multipart/form-data">

            <p:messages id="messages" autoUpdate="true"/>
            <p:panelGrid id="grid">

            <p:row> <p:column>
                <h:outputLabel for="name" value="#{msg['label_book_title']}" />
                <p:spacer width="100" height="10" />
                <p:inputText id="name" value="#{bookBean.book.title}"/>
            </p:column> </p:row>    
            <p:row> <p:column>

               <p:fileUpload id="upload" value="#{bookBean.file}"
                              dragDropSupport="true"
                              update="messages"
                              fileUploadListener="#{bookController.handleFileUpload}" />

            </p:column> </p:row>
            </p:panelGrid>

            <p:toolbar>  
                <p:toolbarGroup align="left">
                    <p:commandButton value="#{msg['button_next']}" action="next" execute="@form" update="@form"/>
                    <p:commandButton value="#{msg['button_cancel']}" action="exit" execute="@form" update="@form"/>
                </p:toolbarGroup>
            </p:toolbar>

        </h:form>
    </ui:define>

    <ui:define name="help">

    <h:outputText value="#{msg['help_add_page']}" /> 

    </ui:define>

</ui:composition>

以上页面的布局:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:sec="http://www.springframework.org/security/tags"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<f:view contentType="text/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>#{msg['application_name']}</title>

        <link rel="icon" href="resources/resources/images/favicon.ico" type="image/x-icon" />
        <link rel="shortcut icon" href="resources/resources/images/favicon.ico" />
        <link rel="stylesheet" href="resources/resources/styles/blueprint/screen.css" type="text/css" media="screen, projection" />
        <link rel="stylesheet" href="resources/resources/styles/blueprint/print.css" type="text/css" media="print" />
        <!--[if lt IE 8]>
            <link rel="stylesheet" href="${request.servletPath}/styles/blueprint/ie.css" type="text/css" media="screen, projection" />
        <![endif]-->
    </h:head>

    <h:body>
        <p:layout fullPage="true">

            <p:layoutUnit id="top" position="north" header="#{msg['application_name']}" size="100" collapsible="true" collapsed="false" resizable="true">
                <ui:insert name="title" />
            </p:layoutUnit>

            <p:layoutUnit id="west" position="west" size="250" header="#{msg['label_help']}" resizable="true" collapsible="true" collapsed="false">
                <ui:insert name="help" />
            </p:layoutUnit>

            <p:layoutUnit id="center" position="center" >
                <ui:insert name="content" />
            </p:layoutUnit>

        </p:layout>

    </h:body>

</f:view>
</html>

有谁知道我做错了什么?非常感谢。

4

2 回答 2

1

您的代码似乎是正确的,我遇到了与 JSF 2.2 和 Primefaces 5.3 相同的问题

我的上传代码是

public void uploadPhoto(FileUploadEvent e) throws IOException {

    UploadedFile uploadedPhoto = e.getFile();

    String filePath =  "TO/THE/PATH";
    byte[] bytes = null;

    if (null != uploadedPhoto) {
        bytes = uploadedPhoto.getContents();
        String filename = FilenameUtils
                .getName(uploadedPhoto.getFileName());
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(filePath + filename)));
        stream.write(bytes);
        stream.close();
    }
    setProduitIMG(uploadedPhoto.getFileName());
    setImageSize(uploadedPhoto.getSize());

    FacesContext.getCurrentInstance().addMessage(
            "messages",
            new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Your Photo (File Name " + uploadedPhoto.getFileName()
                            + " with size " + uploadedPhoto.getSize()
                            + ")  Uploaded Successfully", ""));
}

您可以在托管 bean 中使用它,而无需创建新的 fileBean 您可以与任何托管 bean 类合并 它工作正常

JSF 2.1 OR 2.0 在这种情况下无法使用 spring 集成,您必须升级您的项目,它会正常工作

于 2016-05-11T18:50:42.367 回答
0

我认为问题出在您的<p:commandButton value="#{msg['button_next']}" action="next" execute="@form" update="@form"/>. 使用 AJAX 上传文件是不可能的,所以你需要添加ajax="false"到你的p:commandButton,也许你必须使用一个 actionListener 而不是一个动作,但在后者上,我不确定。您应该在您的上下文中开始尝试来自https://www.primefaces.org/showcase/ui/file/upload/single.xhtml的示例。

于 2013-08-19T12:26:01.137 回答