2012-03-01 105 views
3

我想使用JSF2.0/Primefaces使用<p:fileUpload>上传文件。我还没有找到任何可以帮助我的文档。如何使用JSF/Primefaces上传文件?

我有两个表:Person(name,lastnam....)file(id,path,name,size) ,我想实现的方案是:

  • 用户订阅到我的网站,我救了他的信息,包括文件上传
  • 当文件上传,我要保存我的硬盘上,节省 路径到我的数据库
    (保持用户和他的文件之间的关系)

所以当用户按下按钮保存按钮时,我保存所有这些信息。

这里是我的index.xhtml

<h:form > 
    <p:panel header="Add User" style="width: 500px; font-size: 14px"> 
     <h:panelGrid width="width: 300px;" columns="2"> 


     <h:outputLabel style="font-size: 13px" value="Name" /> <h:inputText value="#{AddPerson.lastname}" /> 
     <h:outputLabel value="LastName"/> <h:inputText value="#{AddPerson.name}" /> 

      <h:outputLabel value="Marital Status"/> 
      <h:selectOneMenu id="status" value="#{AddPerson.status}"> 
      <f:selectItem itemValue="Single" itemLabel="Single"/> 
      <f:selectItem itemValue="Married" itemLabel="Married"/> 
      </h:selectOneMenu> 

      <h:outputLabel value="Bith date "/> <p:calendar value="#{AddPerson.birthdate}" id="popupButtonCal" showOn="button" /> 
      <h:outputLabel value="email"/><h:inputText value="#{AddPerson.email}" /> 
      <h:outputLabel value="mobile"/><h:inputText value="#{AddPerson.mobile}" /> 
      <h:outputLabel value="fax"/><h:inputText value="#{AddPerson.fax}" /> 
      <h:outputLabel value="Job"/><h:inputText value="#{AddPerson.Job}" /> 
      <h:outputLabel value="addresse"/><h:inputText value="#{AddPerson.addresse}" /> 
      <h:outputLabel value="code"/><h:inputText value="#{AddPerson.code}" /> 
      <h:outputLabel value="Country"/><h:inputText value="#{AddPerson.country}" /> 
      <h:outputLabel value="login"/><h:inputText value="#{AddPerson.login}" /> 
      <h:outputLabel value="password"/><h:inputText value="#{AddPerson.password}" /> 

      <h:outputLabel value="CV"/> <input type="file" name="uploaded_file"/> 
      <p:fileUpload fileUploadListener="#{AddPerson...." update="messages" sizeLimit="500000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/> 
      <p:growl id="messages" showDetail="true"/>  // **example :taken from primefaces showcases** 

      <h:commandButton action="#{AddPerson.addUserDB}" value="Add User" /> 

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

,这里是My bean

public void addUserDB() { 
    try { 

     EntityTransaction entr = em.getTransaction(); 
     entr.begin(); 

     Person user = new Person(); 

     user.setNom(lastname); 
     user.setPrenom(name); 
     user.setCodepostal(code); 
     user.setEmail(email); 
     user.setEtatCivil(status); 
     user.setFax(fax); 
     user.setDateNaissance(birthdate); 
     user.setMobile(mobile); 
     user.setAdresse(addresse); 
     user.setPays(country); 
     user.setLogin(login); 
     user.setPassword(password); 

     //**I should also add here the path of the file to the table and save the file on the disc !!!!**   

     em.persist(user); 

     entr.commit(); 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
     System.out.println("Failed"); 
    } finally { 
     em.close(); 
    } 

} 
+2

PrimeFaces有确凿例子展示:http://www.primefaces.org/showcase/ui/fileUploadHome.jsf PrimeFaces也有一个用户指南:http://www.primefaces.org/documentation。 html – BalusC 2012-03-01 19:41:40

+0

你在WEB-INF/lib下添加了两个jar文件吗? common-fileupload.jar和common.io.jar – 2013-10-25 06:12:21

回答

10

哪来的fileUploadListener的实施?我通常只是这样做:

<p:fileUpload cancelLabel="#{msg['cancel']}" update="someComponent" 
fileUploadListener="#{someBean.uploadListener}" 
multiple="false" sizeLimit="1000000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" /> 

然后我的bean有一个方法来处理文件上传事件。事情是这样的:

public void fileUpload(FileUploadEvent event) throws IOException { 
    String path = FacesContext.getCurrentInstance().getExternalContext() 
      .getRealPath("/"); 
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss"); 
    String name = fmt.format(new Date()) 
      + event.getFile().getFileName().substring(
        event.getFile().getFileName().lastIndexOf('.')); 
    File file = new File(path + "catalogo_imagens/temporario/" + nome); 

    InputStream is = event.getFile().getInputstream(); 
    OutputStream out = new FileOutputStream(file); 
    byte buf[] = new byte[1024]; 
    int len; 
    while ((len = is.read(buf)) > 0) 
     out.write(buf, 0, len); 
    is.close(); 
    out.close(); 
} 

保持到刚刚保存和使用,在您的addUserDB()方法中设置相应的用户属性的文件路径参考。所以这是一个两步骤的过程。您首先将文件保存在服务器的某个位置,然后保存您的用户。

+0

我试过这个,但文件保存在D:\ MyDocuments \ MySites \ MySiteExample \ build \ web \ eventImage137.jpg 有没有什么办法不把它保存在BUILD文件夹,但在“真实”的Web文件夹? – vtomic85 2014-06-12 19:43:03

+2

真的,你可以随时随地保存。只需相应地设置路径变量即可。尽管不要对其进行硬编码,因为如果最终在具有不同操作系统的不同服务器上运行此操作,可能会遇到问题。也许使用环境变量或java参数来设置文件将被保存的位置。 – Andre 2014-06-13 12:56:33

+0

嗨@Andre你能澄清谁“使用环境变量或java参数来设置文件将被保存的位置”,请 – 2014-11-24 23:36:10

3

fileUpload有fileUploadListener。请注意,您应该实现逻辑以将文件内容保存到您的后台bean中。

<p:fileUpload fileUploadListener="#{fileBean.handleFileUpload}"> 

public class FileBean { 
    public void handleFileUpload(FileUploadEvent event) { 
     UploadedFile file = event.getFile(); 
     String fileName = file.getFileName(); 
     long fileSize = file.getSize(); 
     InputStream myInputStream = file.getInputstream(); 
     //Save myInputStream in a directory of your choice and store that path in DB 
    } 
}