File upload 如何使用JSF/Primefaces上传文件?

File upload 如何使用JSF/Primefaces上传文件?,file-upload,jsf-2,primefaces,File Upload,Jsf 2,Primefaces,我想使用JSF2.0/Primefaces和上传一个文件。我还没有找到任何可以帮助我的文档 我有两个表:Person(name,lastnam…)和file(id,path,name,size) 我想要实现的场景是: 一个用户订阅了我的网站,我保存了他的信息,包括上传的文件 上传文件后,我想将其保存在磁盘上并保存 进入我的数据库的路径 (保持用户与其文件之间的关系) 因此,当用户按下按钮Save时,我保存所有信息 这是我的index.xhtml <h:form > &l

我想使用JSF2.0/Primefaces和
上传一个文件。我还没有找到任何可以帮助我的文档

我有两个表:
Person(name,lastnam…)
file(id,path,name,size)
我想要实现的场景是:

  • 一个用户订阅了我的网站,我保存了他的信息,包括上传的文件
  • 上传文件后,我想将其保存在磁盘上并保存 进入我的数据库的路径
    (保持用户与其文件之间的关系)
因此,当用户按下按钮Save时,我保存所有信息

这是我的
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>

fileUploadListener的实现在哪里?我通常只是这样做:

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

保留对刚刚保存的文件路径的引用,并将其用于在addUserDB()方法中设置相应的用户属性。所以这实际上是一个两步的过程。首先将文件保存在服务器中的某个位置,然后保存用户。

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
    }
}

公共类FileBean{
公共无效handleFileUpload(FileUploadEvent事件){
UploadedFile=event.getFile();
字符串fileName=file.getFileName();
long fileSize=file.getSize();
InputStream myInputStream=file.getInputstream();
//将myInputStream保存在您选择的目录中,并将该路径存储在DB中
}
}

PrimeFaces有一个带有具体示例的展示:PrimeFaces还有一个用户指南:您是否在WEB-INF/lib下添加了两个jar文件;common-fileupload.jar和common.io.jar我已经试过了,但是文件保存在D:\MyDocuments\MySites\mysitexample\build\web\eventImage137.jpg下。有没有办法不保存在build文件夹中,而是保存在“real”web文件夹中?你可以保存在任何你想要的地方,真的。只需相应地设置path变量。不过,请尽量不要硬编码,因为如果最终在具有不同操作系统的不同服务器上运行,可能会遇到问题。可能使用环境变量或java参数设置文件的保存位置。您好@Andre您能否澄清“使用环境变量或java参数设置文件的保存位置”是谁
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();
}
<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
    }
}