Sending OutputStream to browser and browser resolution

I want to export data from a Rich Faces data table. I created an outputStream from the data in the data table. Now you want to send this OutputStream to the browser and save it. How can i do this?

FileOutputStream stream = new FileOutputStream(new File(PATH));

OutputStream out = myMthodToCreateOutPutStream();

Now how to save this out in the browser.

+4
source share
1 answer

It is not clear where you are reading your data from. You need to create an InputStream to read the data.

Then you first need to configure the response headers to

 HttpServletResponse.setHeader("Content-Disposition", "attachment; filename=datafile.xls"); 

Use any file name you need.

Then set the mime type:

 response.setContentType("application/vnd.ms-excel"); 

Use the desired mime type.

Then you need to use the response object to get its output stream -

 OutputStream outStream = response.getOutputStream(); 

Now write to him:

 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(); 
+12
source

All Articles