• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
response.setContentType("text/plain");

where response is an HttpServletResponse

Francis
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The header you're looking for is Content-Disposition.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic