Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = != -1))
// the data has already been read into buf
String uri = "/HelloWorld.jsp";
RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher(uri);
Urgent!!! please help...
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!