Jump to Real's How-to Main page

Specify the filename to be used for a file sent by a Servlet

Say that your servlet called ""/servlet/GetFile" send back a file to a client request. That file needs to be saved by the client. If you do nothing, the browser will suggest "GetFile" as the filename.

To specify the correct filename

String filename = "abc.dat" ;
File textFile = new File(filename);
response.setHeader("Content-length",Integer.toString(textFile.length()));
response.setHeader("Content-type","application/octetstream");
response.setHeader("Content-disposition","inline; filename=" + filename );

// open the file and write it in the OutputStream
NOTE: For PDF output, see this HowTo
If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]