Jsf 将OutputStream发送到浏览器并让浏览器保存

Jsf 将OutputStream发送到浏览器并让浏览器保存,jsf,richfaces,export-to-excel,Jsf,Richfaces,Export To Excel,我想从RichFaces数据表中导出数据,我已经从数据表中的数据创建了outputStream。现在要将此输出流发送到浏览器并让其保存。我该怎么做 FileOutputStream=newfileoutputstream(新文件(路径)) OutputStream out=myMthodToCreateOutPutStream() 现在,如何将此保存到浏览器中 不清楚您从何处读取数据。您需要创建一个InputStream来读取数据 然后,首先需要将响应头设置为 HttpServletRespon

我想从RichFaces数据表中导出数据,我已经从数据表中的数据创建了outputStream。现在要将此输出流发送到浏览器并让其保存。我该怎么做

FileOutputStream=newfileoutputstream(新文件(路径))

OutputStream out=myMthodToCreateOutPutStream()


现在,如何将此
保存到浏览器中

不清楚您从何处读取数据。您需要创建一个InputStream来读取数据

然后,首先需要将响应头设置为

HttpServletResponse.setHeader("Content-Disposition", "attachment; filename=datafile.xls");
使用您需要的任何文件名

然后设置mime类型:

response.setContentType("application/vnd.ms-excel");
使用所需的mime类型

然后需要使用响应对象获取其outputstream-

OutputStream outStream = response.getOutputStream();
现在写信给它:

byte[] buf = new byte[4096];
int len = -1;

//Write the file contents to the servlet response
//Using a buffer of 4kb (configurable). This can be
//optimized based on web server and app server
//properties
while ((len = inStream.read(buf)) != -1) {
    outStream.write(buf, 0, len);
}

outStream.flush();
outStream.close();

这给了我一个解决问题的提示。。非常感谢