views:

49

answers:

1

I have a JSP page that handles file downloads.

I set the response header like so:

response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename="+fileName);

When the fileName contains spaces (i.e. "Business Report.doc"), the browser's dialog window saves the file as "Business".

I tried using URLEncoder.encode(fileName, "Unicode"); (also tried UTF-8)

but I get "Business+Report.doc" as the result.

I want the final result to be "Business Report.doc"

Any ideas?

Thanks.

+3  A: 

You need to quote it.

response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");

Note that a JSP is essentially the wrong place to handle file downloads. You will risk that the binary file get corrupted with template text. Better use a Servlet for this. Here's a basic example.

BalusC
thanks! I will also try your Servlet solution out.
hmak
You're welcome.
BalusC