views:

36

answers:

1

This is the code snippet I am trying to run. It is working fine locally, but if I try to run it in a unix server, the name of the down loaded file is displayed as the url mapped to the servlet in the web.xml instead of Report.xls. Please help me…

response.reset();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=Report.xls;");
A: 

This is a known IE issue. It indeed completely ignores the filename in the Content-Disposition header and uses the last part of the URL pathinfo as default filename in the Save As dialogue. You should add the filename of the download file as part of the pathinfo. E.g http://example.com/context/reportservlet/report.xls where the servlet is mapped on an url-pattern of /reportservlet/* (note the trailing /*) in web.xml.

If you want to get the specified filename in the servlet so that you can do one and other more dynamically, then you can use HttpServletRequest#getPathInfo() for this.

String filename = request.getPathInfo().substring(1);
// Substring gets rid of leading `/`.

See also:

BalusC
Wow!!! Superb. It is working. Thanks a lot
Priya
You're welcome. Don't forget to mark the answer accepted. See also http://stackoverflow.com/faq :)
BalusC