• Post Reply Bookmark Topic Watch Topic
  • New Topic

download text file (CSV) from database to browser through servlet  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to stream both character and binary data from the database through a servlet to the browser, but this problem seems to be a slightly different case. This is not HTML or an image stored in the database--it needs to be a text file. No difference from the others in the database...I'll just store text in a CLOB.

But you know how if you have a file on your webserver and you someone clicks on a URL to it and the browser pops up with a file download box? How do you get the browser to do that if your servlet is pulling the text file out of the database?

I see that there are products on the market for stuff like this...is it a nontrivial solution? If there's a good way to trick the browser into thinking that I'm giving it an already constructed file, is there a way to also tell it what the file's name is, even though it's not actually a constructed file? I mean, if I have this String/bytes out of the database, I want the browser to think that the CSV file is named something.csv so that the user has a better understanding of what to do with it. Thanks...
[ December 23, 2004: Message edited by: Stephen Huey ]
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37249
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen,
Have your servlet set a header in the response with the MIME type for a text file. You can also put the suggested default file name to save in there.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that there's a method to set a response header, but I can't figure out what header it is for this suggested file name. I've been Googling around--just not in the right place. I got to a dead end here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.2
 
Francis Shillitoe
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.setContentType("text/plain");

where response is an HttpServletResponse

Francis
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The header you're looking for is Content-Disposition.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!