Problem with encoding StreamResult () by utf-8
Hi All,
In my code , I receive a OutputStream from response() of HttpServletResponse and the resultant output stream is passed to transform to a StreamResult constructor. My stream consist of a Multilingual string which is coming as ? .
Here is my code.
public boolean execute(
String pipelineName, Map[] pipelineParams, OutputStream output ) {
........
.........
........
.......
TransformerFactory f = TransformerFactory.newInstance();
Result r = new StreamResult(output);
t.transform(Source,r);
}
Also I tried with PrintWriter object for the StreamResult constructor.
try{
OutputStreamWriter out = new OutputStreamWriter(output,"UTF-8");
PrintWriter printwriter = new PrintWriter(out);
TransformerFactory f = TransformerFactory.newInstance();
t.transform(Source,new StreamResult(printwriter));
}catch(java.lang.Exception ec){
logger.log( Level.SEVERE, "Encoding Exception Happened :Execution failed", ec);
}
My output steam consist of streams that can not be converted to character stream ,print writer is not a best option. How do i set the OutputStream in utf-8 encoding.
Thanks,
Prabi
Hi
>>My stream consist of a Multilingual string which is coming as ? .
Reason of showing ? instead of original characters:
1 - Wrong selection of encoding e.g. UTF-16, GB2312, BIG5 and etc
2 - Wrong selection of font. In case of showing it in any GUI component [Select Unicode based font e.g. Arial Unicode MS]
Things to take in notice:
===================
1 - Now first of all figure out that, response given to you is UTF-8 based or not. My guess is that it is UTF-16 based. So first of all try to get response in UTF-16.
2 - If your response is UTF-8 based then the characters of the respective language should be in \uXXXX. Otherwise you'll lost the characters.
3 - If your response is in actual character representation of that respective language then it should be decoded using UTF-16 encoding instead of UTF-8.
Alternate Solution
==============
String m_strToEncoding = "UTF-16";
//String m_strToEncoding = "UTF-8";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, m_strToEncoding));
TransformerFactory f = TransformerFactory.newInstance();
t.transform(Source,new StreamResult(writer));
//In case of multilingual strings or characters it is good practise to use "BufferedWriter"
//Use above code, instead of using:
OutputStreamWriter out = new OutputStreamWriter(output,"UTF-8");
PrintWriter printwriter = new PrintWriter(out);
TransformerFactory f = TransformerFactory.newInstance();
t.transform(Source,new StreamResult(printwriter));
Regards,
KS
k5ma at 2007-7-14 23:07:39 >
