gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:22:02 24 4
gpt4 key购买 nike

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

我有两张 table :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();
    }

    }

    最佳答案

    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() 方法中设置相应的用户属性。所以这实际上是一个两步过程。您首先将文件保存在服务器中的某个位置,然后保存您的用户。

    关于file-upload - 如何使用 JSF/Primefaces 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9515365/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com