How to set response header in JAX-RS so that user sees download popup for Excel?

Posted by masato-san on Stack Overflow See other posts from Stack Overflow or by masato-san
Published on 2011-01-07T01:31:33Z Indexed on 2011/01/07 4:54 UTC
Read the original article Hit count: 265

Filed under:
|
|
|
|

I wrote code that generate Excel file using REST JAX-RS and I confirmed that the generated Excel file is in GlassFish server directory.

But my goal is when user click on the button (which generate Excel .xls), I want download popup to show up asking user whether to save or open the .xls file just like any other web services doing for downloading any type of files.

According to my search, the step is:

  1. generate Excel .xls (DONE)

  2. write the excel to stream

  3. in JAX-RS file, set response header to something like,

    String fileName = "Blah_Report.xls"; response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

My question is I'm doing all of this in JAX-RS file and I don't have HttpServletResponse object available.

According to the answer from Add Response Header to JAX-RS Webservice

He says:

You can inject a reference to the actual HttpServletResponse via the @Context annotation in your webservice and use addHeader() etc. to add your header.

I can't really figure what exactly that means without sample code..

© Stack Overflow or respective owner

Related posts about java

Related posts about rest