Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

forcing a download rather than viewing in the browser

843836Aug 11 2004 — edited Aug 17 2004
Short version of the question: Is there a way that my JSP program can tell the browser that the output it is about to receive should be downloaded to the user's hard drive rather than displayed in the browser window? i.e. like would happen if the user right-clicked in MSIE.

Longer version: I have an app where we want the users to download PDF files to their local hard drives from our web site. We have to do some processing with each download (mostly recording the fact that the user has just downloaded this file), so in the relevant section of the system, the user clicks on a link which is really to a JSP program, we do our processing, update the database, and then do a response.sendRedirect to give them the desired file.

But there are two ugly parts to this. One, if the user has Acrobat installed, then left-clicking on our link will bring the file up in their browser window rather than downloading it. So they have to right-click on the link. Okay, annoying, but we put some instructions about that on the screen, and to date I've been accepting that as a necessary evil. But now I just came across a second ugly: If there's a problem, I'd like to return a screen with some sort of error message. But we've just told them to right click, so they end up saving the error message to their hard drive rather than seeing it.

So ... is there any way within a JSP program that I can pass a message to the browser that tells it that this file should be downloaded rather than displayed in the browser window? I tried doing a setContentType("application/octet-stream") but this did nothing: I'm not sure if the browser ignores it because it figures out that this file is PDF, or if it gets lost over the redirect, or what.

Note: Solutions may be limited by the fact that most of the files to be downloaded are not on the same computer that my JSP app is running on: We're pulling documents from many web sites scattered across our organization.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2004
Added on Aug 11 2004
2 comments
595 views