Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setContentType and getOutputStream

 
Kyle Tang
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a question about setContentType() and getOutputStream(), getWriter().
setContentType() should be called before calling getWriter(), as is said on the API spec. Should we call setContentType() before calling getOutputStream() too?
Thanks!
Kyle
 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I believe.
 
Raj Paul
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, getOutputStream is used to write binary data. so i hope there will not be the need to call setContentType() method.
Raj Paul
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is always a need to set the content type, especially when writing binary data. After all, if you just present the browser with a splotch of binary gloop, how is it supposed to know whether it's a JPEG image (image/jpeg), GIF image (image/gif), PNG image (image/png), AVI movie, QuickTime movie, MIDI file, Word document, or some format it doesn't know and needs to pop up a "Save As..." dialog box for?
- Peter
 
Marcelo Castro
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter is right. Givin� an additional information: if you don�t specify the method setContentType, the type "text/html" will be evaluated automatically
keep studying
 
Kyle Tang
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
My understanding now is,
1. if need to call setContentType() and getWriter(), then always call setContentType() first. The reason is setContentType() may change the charset, which affect the PrintWriter.
2. setContentType() doesn not need to be called before calling getOutputStream()
3. if no setContentType() called, content type is "text/html;charset=iso-8859-1".
Kyle
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the summary Kyle. I believe I may have misinterpreted RP's reply -- rereading this thread, it is clear that he meant there is no need to call setContentType before calling getOutputStream(). Which is correct (although you should call it before writing any great quantities of data to this stream; in fact it is good style to set the content type before writing any data to the output stream).
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic