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();
}
}
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 theUploadedFile
, it is possible to obtain theInputStream
(http://www.primefaces.org/docs/api/3.4/org/primefaces/model/UploadedFile.html).– Wakim