Java webapp: adding a content-disposition header to force browsers "save as" behavior

Posted by WizardOfOdds on Stack Overflow See other posts from Stack Overflow or by WizardOfOdds
Published on 2010-03-08T23:23:55Z Indexed on 2010/03/08 23:36 UTC
Read the original article Hit count: 559

Filed under:
|
|
|
|

Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a resource to be downloaded (rather than displayed) in a browser can use the Content-Disposition header like this:

Content-Disposition: attachment; filename=FILENAME

Even tough it's only defined in RFC2183 and not part of HTTP 1.1 it works in most web browsers as wanted.

So from the client side, everything is good enough.

However on the server-side, in my case, I've got a Java webapp and I don't know how I'm supposed to set that header, especially in the following case...

I'll have a file (say called "bigfile") hosted on an Amazon S3 instance (my S3 bucket shall be accessible using a partial address like: files.mycompany.com/) so users will be able to access this file at files.mycompany.com/bigfile.

Now is there a way to craft a servlet (or a .jsp) so that the Content-Disposition header is always added when the user wants to download that file?

What would the code look like and what are the gotchas, if any?

© Stack Overflow or respective owner

Related posts about java

Related posts about content-disposition