无需在服务器保存文件的动态文件下载 [英] Dynamic file download without saving file in the server
问题描述
我使用Apache POI库在多个Excel文件做一些操作。
我想下载Excel报表没有地方存放在服务器中。
我使用Struts 2,需要送入的InputStream
而POI工作簿需要一个的OutputStream
写入文件将数据导入。
任何帮助将是巨大的。
既然你已经知道你需要一个流的结果:
我使用Struts 2,需要送入文件中的
的InputStream
块引用>//吸气剂
私人的InputStream的InputStream;和你已经知道如何使用POI创建一个Excel:
POI工作簿需要一个
的OutputStream
来将数据写入。
块引用>公共字符串的execute(){ // 东东 ByteArrayOutputStream BAOS =新ByteArrayOutputStream(); //填写使用Excel内容的OutputStream
workbook.write(BAOS);那么唯一缺少的部分是如何将POI的OutputStream的转换成Struts2的流结果的InputStream的。这是比其他人更容易..:
//创建一个从OutputStream的提取的一个字节输入流
的InputStream =新ByteArrayInputStream进行(baos.toByteArray()); 返回成功;
}I am using Apache POI libraries to do some operation on multiple excel files.
I'm trying to download the excel report without storing it somewhere in the server.
I am using Struts 2 which needs the file fed into a
InputStream
while POI Workbook needs aOutputStream
to write the data into.Any help would be great
解决方案Since you already know you need a Stream result:
I am using Struts 2 which needs the file fed into a
InputStream
// With Getter private InputStream inputStream;
and you already know how to create an Excel with POI:
POI Workbook needs a
OutputStream
to write the data into.
public String execute(){ // stuff ByteArrayOutputStream baos = new ByteArrayOutputStream(); // fill the OutputStream with the Excel content workbook.write(baos);
then the only missing piece is how to convert the POI's OutputStream into the Struts2 Stream result's InputStream. And this is easier than the rest..:
// Create an Input Stream from the bytes extracted by the OutputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); return SUCCESS; }
这篇关于无需在服务器保存文件的动态文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!