Sending Files To Browser in JSP

pradeep's Avatar author of Sending Files To Browser in JSP
This is an article on Sending Files To Browser in JSP in JSP.
Code: JSP
// fetch the file
String filename = "companySecret.txt";
String filepath = "C:\\";

response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + '"'); fileInputStream = new + filename);

int i;
while (( != -1)

parvez.yu's Avatar, Join Date: Feb 2008
Ambitious contributor
ok got it
gkumar's Avatar
I think it is a stupid problem. But I do not know how it can be cracked:
I want need the user to be able to download a file that is stored somewhere in the server.
I found some solutions like:
If I integrate this code in my action or actionListener method it does not works.
The reason is that the result for the response
HttpServletResponse response =
( HttpServletResponse ) context.getExternalContext().getResponse();
is always null.
Have anyone a tip?