9

我正在尝试像 primefaces 用户指南中记录的那样工作,以及在那里建立的一些帖子。

在 JSF primefaces 中上传文件

环境是:javaee full + jpa + jsf 2.2 + primefaces 4 + glassfish v4

我再次发帖,因为我已经尝试了我在网上找到的每一个样本和建议,但没有成功。

我能够使用:event.getFile.getFileName 获取文件上传的名称,但内容始终为空

- - - - - - -更新 - - - - - - - - - - - - - -

我的 xhtml 页面:

<h:form enctype="multipart/form-data">

                <p:outputLabel value="La photo :"/>
                <p:fileUpload fileUploadListener="#{personController.upload}"
        mode="advanced" 
        update="messages"
        sizeLimit="100000" 
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
                <p:growl id="messages" showDetail="true"/>

            </h:form>

我的托管豆:

@Named(value = "personController")
@SessionScoped
public class PersonController implements Serializable {

    /**
     * Creates a new instance of PersonController
     */
    @Inject
    private PersonneFacade personneService;
    private Personne current;
    private Personne newPerson;
    private List<Personne> personnes;


    public PersonController() {
    }

    public List<Personne> getAll(){
        return personneService.findAll();
    }


    public void upload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file

        System.out.println(event.getFile().getFileName());
        System.out.println("le fichier " + event.getFile().getContents());
        newPerson.setPhoto(event.getFile().getContents());
    }

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

     <!-- ############################################# -->
<!-- # File upload                                      # -->
<!-- ############################################# -->
<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>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>


</web-app>

当然我的 pom.xml 上有:

<dependencies>
        <dependency>  
    <groupId>org.primefaces</groupId>  
    <artifactId>primefaces</artifactId>  
    <version>4.0</version>  
</dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>

当我尝试上传时,我得到了 glassfish 输出日志:

INFO:   mdmx.jpg
INFO:   le fichier null

有什么想法吗?

4

2 回答 2

15

我已经解决了这个问题,如下所示:

使用时上传对我有用:IOUtils.toByteArray(file.getInputstream());

环境是:Java EE 7(JPA,EJB,JSF 2.2.4),Glassfish V4,Primefaces 4

我在这里给出了完整的例子,以供重用。

第一个:上传.xhtml

<h:form enctype="multipart/form-data">
    <p:fileUpload fileUploadListener="#{personController.handleFileUpload}" />
            <p:growl id="messages" showDetail="true"/>
</h:form>

第二:PersonController.java(jsf托管bean):包com.sos.fso.grh.controllers;

import com.sos.fso.grh.entities.Person;
import com.sos.fso.grh.services.PersonFacade;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.UploadedFile;

@Named
@SessionScoped
public class PersonController implements Serializable{

@Inject
private PersonFacade personService;

private Person current;
private Person newPerson;
private List<Person> personnes;

public void handleFileUpload(FileUploadEvent event) throws IOException {

    UploadedFile file = event.getFile();
    System.out.println(file.getFileName());

    byte[] foto = IOUtils.toByteArray(file.getInputstream());
    System.out.println(foto);
    newPerson.setPhoto(foto);
 //application code
}

}

第三:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>
         <context-param>   
 <param-name>primefaces.UPLOADER</param-name>   
 <param-value>auto</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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
        </web-app>

如果您有一个实体 bean“人”,例如带有 @Lob 二进制属性,您可以用字节数据填充它。保存图像(或文件,如果需要)。

恢复它

view.xhtml

    <!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"   
 xmlns:h="http://java.sun.com/jsf/html"   
 xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
    <h:form>
    #{personController.current.FName}

        <p:panelGrid columns="2">
        <p:outputLabel value="Nom :"/>
        <h:outputText value="#{personController.current.FName}" />

        <p:outputLabel value="Prénom :"/>
        <h:outputText value="#{personController.current.LName}" />

        <p:outputLabel value="Date de naissance :"/>
        <h:outputText value="#{personController.current.birthDate}" />

        <p:outputLabel value="photo :"/>
        <p:graphicImage value="#{personController.byteToImage(personController.current.photo)}" />
        </p:panelGrid>
        <p:commandLink value="retour a la liste" action="#{personController.showList}"/>
        </h:form>

</h:body>
</html>

以及恢复图像的 PersonController 方法(此代码是来自 stackOverFlow 的 BalusC )

public DefaultStreamedContent byteToImage(byte[] imgBytes) throws IOException {
ByteArrayInputStream img = new ByteArrayInputStream(imgBytes);
return new DefaultStreamedContent(img,"image/jpg");
}

谢谢大家帮助我。

再次感谢

于 2013-11-27T15:31:51.303 回答
0

将Apache-Commons IOFileupload API 包含到您的应用程序中 :

于 2013-08-05T05:18:12.177 回答