MOhamed
MOhamed

Reputation: 77

How to import file in a directory using jsf

Hi all I'm new to Java development, and I'm really confused about this. im doing an web app and my problem is how to import file and put it in i directory. i have created the xhtml file :

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:jsf="http://xmlns.jcp.org/jsf"
    xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/template/template.xhtml">
<ui:define name="title">2G</ui:define>


    <ui:define name="content">
    <h:form>

    <h1> <font color="orange" size="7" > 2G</font></h1> 


    </h:form>
    <h2 >Choose 2 files </h2>
    <h:form>
            <p:fileUpload fileUploadListener="#{import_2G.save()}"
                mode="advanced" dragDropSupport="true" update="messages"
                sizeLimit="100000000000" allowTypes="/(\.|\/)(xls)$/" />

            <p:growl id="messages" showDetail="true" />
        </h:form>

</ui:define>
</ui:composition>

and this is the bean file :

    @ManagedBean
@RequestScoped
public class Import_2G {
    public Import_2G() { }
    @EJB
    private GestionCellRef2GLocal gestionCellRef2GLocal;

    private UploadedFile uploadedFile;

    public void save() throws IOException {
        GestionCellRef2GRemote t = null;


            Path folder = Paths.get("C:\\Upload");
            String filename = FilenameUtils.getBaseName(uploadedFile.getFileName());
            String extension = FilenameUtils.getExtension(uploadedFile.getFileName());
            Path file = Files.createTempFile(folder, filename + "-", "." + extension);
            if (file != null) {
                FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " was uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }

            try (InputStream input = uploadedFile.getInputstream()) {
                Files.copy(input, folder, StandardCopyOption.REPLACE_EXISTING);


    }
    }

}

any help guys ?

Upvotes: 0

Views: 1500

Answers (1)

user5835329
user5835329

Reputation:

First start reading about naming conventions in Java. If you not respect the naming conventions and use underscores, scores and things like that, you will have some troubles.

Second, you forgot the enctype. When you wanna upload binary data, you must have put the attribute: enctype="multipart/form-data". Let's build a file upload.

First your form:

<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{import2G.file}"
                  mode="advanced" dragDropSupport="true"
          sizeLimit="100000000"
          allowTypes="/(\.|\/)(xls)$/"
          update="messages"
          fileUploadListener="#{import2G.save}" />
</h:form>

<p:growl id="messages" showDetail="true" />

And your backing bean:

public void save(FileUploadEvent e) {
    FileUpload file = event.getFile();
    String fileName = file.getFileName();
    String contentType = file.getContentType();
    byte[] content = file.getContents();
    saveFile(content);
}

private void saveFile(byte[] data) {
    FileOutputStream fos = new FileOutputStream(DIR_NAME);
    fos.write(data);
    fos.close();
}

Look the listener in the form; use import2G.save instead import2G.save(), this because a FileUpload parameter is passed to the listener in runtime.

Upvotes: 1

Related Questions