aspose file tools*
The moose likes Servlets and the fly likes Different behavior between getWriter and getOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Different behavior between getWriter and getOutputStream" Watch "Different behavior between getWriter and getOutputStream" New topic
Author

Different behavior between getWriter and getOutputStream

Ted Bell
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
Hi all,

I have read the documentation and different postings which state that when sending character data, using the PrintWriter from response.getWriter() is appropriate, and when sending binary data, using the OutputStream from response.getOutputStream should be used. I actually have character data to be sent in the form of a CSV file, and there is a difference in how well Excel handles the file, based on which of the above methods is used. This:




Causes Excel to fail to recognize special characters like the Euro symbol. While this:




Works just fine. I'm all for using the OutputStream, but I would like to know exactly what the nature of the difference is. Is using getWriter causing the container to do some sort of encoding on the content?

Thanks for any information.
Ted Bell
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
More info: We are using WebLogic 8.1 and the content being sent is generated from a Velocity template.
Pavel Cherkashin
Ranch Hand

Joined: Mar 04, 2005
Posts: 47
Here is the passage from the API Documentation

public java.io.PrintWriter getWriter() throws java.io.IOException
Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.


Is that clear?


Pavel Cherkashin - <br />SCJP, SCWCD, SCDJWS, SCBCD, SCEA, ...<br />www.linkedin.com/in/pcherkas
Ted Bell
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
That would make sense if I were not setting the character encoding, but I am:

response.setContentType("application/ms-excel;charset=UTF-8");


In fact a line immediately after this with a call to resposne.getCharacterEncoding() returns "UTF-8".
Ted Bell
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
Apologies if I am missing something here, but any information (even an educated guess) would be appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Different behavior between getWriter and getOutputStream
 
Similar Threads
Printing contents
Jsp to Excel
from where to download org.apache.poi.hssf.usermodel package???????
UNIX line break problem
application/vnd.ms-excel is not working for content type?