How to read a file sent by user using FileInputStream?

2

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

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

The method follows:

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();
        }
    }
    
asked by anonymous 21.07.2014 / 20:56

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 site: link

On your webpage 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 look 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 do this sending in an easier way and in the "UploadedFile" object itself you can search the path of it.

    
21.07.2014 / 21:16
0

One of the XSSFWorkbook constructors gets a InputStream as a parameter. That being said, you can change FileInputStream to a InputStream type that works best for you, like 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 reaches the server as InputStream and so you can pass it directly to the XSSFWorkbook constructor.

    
24.07.2014 / 18:12