aspose file tools*
The moose likes Servlets and the fly likes How do I download a text file from a servlet onto a client machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I download a text file from a servlet onto a client machine" Watch "How do I download a text file from a servlet onto a client machine" New topic
Author

How do I download a text file from a servlet onto a client machine

raswal rak
Greenhorn

Joined: Jul 01, 2002
Posts: 1
Hi,
I have to download files from the server on to the client machine,
I have written the code, which gives the dialog box asking for location to download the file to on the client machine, after giving the file name and location and then pressings save,
It saves the file but gives the following errors,
1) The IE browser ends up thinking it still is waiting on something, so the hourglass never goes away, and the globe keeps on spinning.
2) Secondly the other links on that page becomes unoperational., and if I click on any other links it gives the error IllegalStateException: response is already committed.
The code is shown below :
response.setContentType ("application/x-filler");
response.setHeader ("Content-Disposition", "attachment;filename=abc.txt"");
ServletOutputStream op = response.getOutputStream ();
while ((in != null) && ((length = in.read(buf)) != -1))
{
// the data has already been read into buf
op.write(buf,0,length);
}
op.close();
String uri = "/HelloWorld.jsp";
RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher(uri);
Urgent!!! please help...
thanks
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
You can't write to the response AND forward the request. You have to do one or the other. As soon as you call response.getOutputStream() you have committed the response.
Is the file on your web server? If so, wouldn't the simple solution be to have your link point to the file on the web server? IE will automatically pop up a dialog that asks the user if they want to open the file or save it to disk. If they choose save, then the file dialog will ask for a directory.

Hope this helps!
Erick
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How do I download a text file from a servlet onto a client machine