Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving a file to clients machine  RSS feed

 
sridhar lakka
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks in advance.
I have a jsp report page.When user clicks Save File button of that JSP,then that file should save to clients machine.
Using FileOutputStream we can save to our war location but i want to save at clients prefered location.
Please help me.
Thanks a lot,
Sree
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write the contents of the FileOutputStream to the ServletOutputStream. That way it'll arrive at the users browsers, where he can decide where to save it.
 
sridhar lakka
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
Could you please tell me in some what detail, if possible with some example line of code.

Regards,
Sree
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet normally writes HTML to its output stream. (A JSP is converted into a servlet that does exactly the same thing.) You can get data from any source, eg read an Excel spreadsheet from disk, and write it to the same stream. When it gets to the browser the user will get a prompt for what they want to do with the file, open, save, etc.

There's one more step, I think. You need to set the MIME type of the data. Look for methods that let the servlet set headers. HERE are some mime types. You might set it to "text/plain" or "application/msword" or something. Google for more info on the mime type header and possible values.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!