• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Outputstream the file through http response

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the requirement like this
1) I have to create a file which would contain data from DB
2) and outputstream this file to http response.

eg: if the request is "http://localhost/abcd/product.csv "
then the user should save the file from his browser.

I have never worked on IO object and I have little knowledge about FileWriter, OutputStreams. Not sure If I have to use Objectoutstream for this purpose ?.

I could manage Mime type, buffer and other properties.

I would really appreciate if some one could help me on this.





 
Anandha Loganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found solution but Do you have an elegant way to do this code.



>
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks about right to me. It a good idea to set the content length of the response as well. I've run into trouble with that before, but it was only when sending the response to a non-standard browser. The normal browsers seemed happy enough with the 0 content length.

If you expect the file to be large, you could read it a chunk at a time and write each chunk immediately to the output stream. That avoids having the whole file resident in a byte array. Actually, unless you need the file for other reasons, you could just let the CSVWriter() write it directly to the servlet output stream (well, through a OutputStreamWriter anyway). In that case, you probably won't know the size though, so you have to hope a 0 content length won't freak out your client.
 
Anandha Loganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't get you. How do we use OutputStreamWriter?.

If you could provide me the example code then I would appreciate that.

Thanks
Ananda
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I just meant you needed an OutputStreamWriter to fit your interface. Something like this:

>
 
Anandha Loganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrote the code in a better way.

>
 
Anandha Loganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg thanks for your help.

 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. IIRC, you don't want to close the servlet output stream though. Your close() call will chain all the way through. If it doesn't cause you any problems though, I guess it's OK.
 
reply
    Bookmark Topic Watch Topic
  • New Topic