Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How send byte stream? (Instead of pointing to file)  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web app (JSP and servlets) that will take requests for files. It will create the bytes for the files on the fly and then I want it to send the bytes to the browser/requester without having to save them to disk and point to that file. How would I do this?
I can't use out.print( bytes ) because the writer for an HttpServletResponse is a PrintWriter and only takes character information, not bytes. How would I instead, change the content-type of the page I'm serving, and then send the bytes of the in-memory file?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very simple with a servlet. Just use the response getOutputStream() method instead of getWriter(). You will probably also want to set the Content-type and Content-length response headers to suit the data you are sending.
See the javax.servlet.ServletResponse Javadocs.
Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!