0

I'm making an Excel file with Apache POI, and I'm saving it into a FileOutputStream.

In XHTML, I want to download this file with <p:fileDownload> but it needs a StreamedContent to work.

I want to know how to parse or cast my FileOutputStream into a StreamedContent to download it with PrimeFaces.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47

2 Answers2

1

Here is solution without saving file to disk:

    HSSFWorkbook workbook = new HSSFWorkbook();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    workbook.write(outputStream);

    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    StreamedContent streamedContent =  new DefaultStreamedContent(inputStream, "application/xls", "output.xls");
Fryta
  • 593
  • 4
  • 6
-2

I had to save my file into disk, and read it again later with a File type to save it into a StreamedContent.

The code:

HSSFWorkbook libro = new HSSFWorkbook();
FileOutputStream elFichero = new FileOutputStream("c:/Temp/MyExcel.xls");
        libro.write(elFichero);
        elFichero.close();
        libro.close();
        File fil = new File("c:/Temp/MyExcel.xls");
        StreamedContent excel = new DefaultStreamedContent(new FileInputStream(fil), new MimetypesFileTypeMap().getContentType(fil), "MyExcel.xls"));

later i could get my excel file by the <p:commandButton>.

Anyways i was looking for a way to try not to save it to disk before download...