views:

40

answers:

2

Hello,

I'm creating a java web application runing on a Tomcat server.

One of the functions fill in a StringBuffer variable with data.

At the end, I would like to propose the user to download the generated content packaged in a text file. This without having to store the file on the server.

I've been searching for a code snippet but couldn't find anything corresponding ...

I hope I've been clear enough on my problem.

Thanks in advance,

+1  A: 

See Making A Download Servlet

Don't forget to add the servlet to your web.xml.

stacker
A: 

You have to send a content-type along with the response, so that the browser knows what to do with the body of the response. Normal text has the content-type text/plain, html is text/html. Images are image/gif and so on. For an unknown mime type you normally set "application/octet", which afaik every browser treats as a download. But I recommend to use the propery content type, so the browser might start a matching application to handle the content (e.g. Office for Documents or XML Editor for XML Files ..)

To send a filename along, which the browser suggests for saving, use the following header (example):

Content-Disposition: attachment; filename="downloaded.pdf"

For sending custom headers, use the setHeader() method in the response object.

ZeissS