Jsf FileOutputStream到StreamedContent

Jsf FileOutputStream到StreamedContent,jsf,primefaces,download,fileoutputstream,Jsf,Primefaces,Download,Fileoutputstream,我正在用ApachePOI制作一个Excel文件,并将其保存到FileOutputStream中 在XHTML中,我想用下载此文件,但它需要StreamedContent才能工作 我想知道如何将我的FileOutputStream解析或转换为StreamedContent以使用PrimeFaces下载。我必须将文件保存到磁盘中,然后使用文件类型再次读取,以将其保存到StreamedContent中 守则: HSSFWorkbook libro = new HSSFWorkbook(); File

我正在用ApachePOI制作一个Excel文件,并将其保存到
FileOutputStream

在XHTML中,我想用下载此文件,但它需要
StreamedContent
才能工作


我想知道如何将我的
FileOutputStream
解析或转换为
StreamedContent
以使用PrimeFaces下载。

我必须将文件保存到磁盘中,然后使用文件类型再次读取,以将其保存到StreamedContent中

守则:

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"));
后来我可以通过
获取我的excel文件


无论如何,我一直在寻找一种方法,在下载之前尽量不将其保存到磁盘…

以下是不将文件保存到磁盘的解决方案:

    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");

请参阅DefaultStreamContent需要一个FileInputStream,但我已经有了一个FileOutputStream。请寻找将outputstream转换为InputStream的方法提示:尝试使用ByteArrayOutputStream。。。很容易获取字节[]并将其传递到ByteArrayInputstreamWhy StreamedContent?直接写下来回应就行了。哪个对解决你的问题最有帮助?或