How to read a user-submitted file using Fileinputstream?

Asked

Viewed 1,136 times

2

I would like the user to send a file . xls to the system and from this would be created a database. The method I have is working, but the parameter that Fileinputstream receives is a String indicating the file path. Only the system will be used by different machines and the uploaded file will have different paths.

What I would like to know is how I make the user choose, from his machine, the file that should be read by the system, rather than the system picking the file from a specific path (static).

Follows the method:

public void convertePlanilhaEmAlunos() {

        try {

            // ALTERAR O PATH DE ACORDO COM O LOCAL DA PLANILHA
            FileInputStream file = new FileInputStream(
                    "C:\\Users\\Desktop\\teste.xls");

            // Get the workbook instance for XLS file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            // Get first sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rowIterator = sheet.iterator();

            while (rowIterator.hasNext()) {

                Row row = rowIterator.next();
                if (row.getRowNum() < 7) {
                    continue;
                }

                for (@SuppressWarnings("unused")
                Cell cell : row) {
                    aluno.setNome(String.valueOf(row.getCell(0)));
                    aluno.setDataNascimento(String.valueOf(row.getCell(1)));
                    aluno.setRg(String.valueOf(row.getCell(2)));
                    aluno.setNomeMae(String.valueOf(row.getCell(3)));
                    aluno.setRgMae(String.valueOf(row.getCell(4)));
                    aluno.setNomePai(String.valueOf(row.getCell(5)));
                    aluno.setCurso(String.valueOf(row.getCell(6)));
                    aluno.setTurma(String.valueOf(row.getCell(7)));

                }

                if (aluno.getNome() == "") {
                    break;
                }

                System.out.println(aluno.getNome());

                criaAluno();

                aluno = new Aluno();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1

    Why don’t you use the component Upload from Primefaces to receive the user file? From a look at http://www.primefaces.org/showcase/ui/file/upload/basic.xhtml. From the UploadedFile, it is possible to obtain the InputStream (http://www.primefaces.org/docs/api/3.4/org/primefaces/model/UploadedFile.html).

2 answers

1

You can add the primefaces library to your project and use the Uploadfile component to do this. Download the primefaces library directly on the website: http://www.primefaces.org/

On your website it will look like this:

<h:form enctype="multipart/form-data">
  <p:growl id="messages" showDetail="true" />

  <p:fileUpload value="#{fileUploadView.file}" mode="simple" disabled="true" />

  <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" disabled="true" />
</h:form>

And in Java it will stay like this:

public class FileUploadView {

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void upload() {
    if(file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

}

With that you can make this upload easier and in the object itself "Uploadedfile" you can search for its path.

0

One of the builders XSSFWorkbook gets a InputStream as parameter. That being said, you can change the FileInputStream by a type of InputStream serve you better, like the BufferedInputStream or any other.

It depends on your need and how the user will send the file to the server. I believe the file already arrives at the server as InputStream and so you can pass it directly to the builder of XSSFWorkbook.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.