迷失在翻译中 - 编码
我目前正在尝试处理从 J2me 应用程序到 Websphere 6 服务器的 http post 请求,该服务器通过 MQ 消息传递与其他系统进行通信。队列字符集是Cp037,从队列中检索的消息是Cp1256。
一切工作正常,直到我尝试以阿拉伯语向 j2me 客户端发送响应,如下所示:
String respStr = new String(orginalMsg, "Cp1256");
response.getOutputStream().write(bytes);
servlet 的响应设置为:
- 字符编码: UTF-8
- 内容类型: text/plain;charset=UTF-8
我随后在 J2me 中阅读了响应:
inputStreamReader = new InputStreamReader(connFact.getInputConnection(),"UTF-8");
buffer = new StringBuffer((int) length); // If no length, default to 256 buffer
char[] data = new char[128];
int total = 0x00;
int read = -1;
do
{
read = inputStreamReader.read(data);
if (read > 0x00)
{
total += read;
buffer.append(data, 0x00, read);
}
} while (read != -1 && !cancel);
我正在 WTK 模拟器中运行代码,我知道该模拟器支持 UTF-8 阿拉伯字符,因为我可以在表单上显示编码字符串。
但是,当仅显示响应结果时?显示字符。
我尝试以编程方式进行转换,而不是让 Websphere 进行隐式转换,但得到了相同的结果。
任何指示将不胜感激。
I am currently trying to process a http post request from a J2me application to a Websphere 6 server which communicates with other systems via MQ messaging. The Queue character set is Cp037 and the message retrieved from the queue is Cp1256.
Everything works fine up to the point where I try to send a response in Arabic to the j2me client as such:
String respStr = new String(orginalMsg, "Cp1256");
response.getOutputStream().write(bytes);
The reponse from the servlet is set with:
- Character Encoding: UTF-8
- Content Type: text/plain;charset=UTF-8
I subsequently read the response in J2me with:
inputStreamReader = new InputStreamReader(connFact.getInputConnection(),"UTF-8");
buffer = new StringBuffer((int) length); // If no length, default to 256 buffer
char[] data = new char[128];
int total = 0x00;
int read = -1;
do
{
read = inputStreamReader.read(data);
if (read > 0x00)
{
total += read;
buffer.append(data, 0x00, read);
}
} while (read != -1 && !cancel);
I am running the code in the WTK emulator which I know supports UTF-8 arabic characters as I can display harcoded strings on a form.
However when it comes to display the result from the response only ? characters are displayed.
I have tried to do the conversion programmatically instead of letting Websphere do the implicit conversion but I get the same result.
Any pointers would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的评论中,您暗示您正在这样做:
如果您从队列中获取字符数据(字符串/字符数组)(
txtMsg.getText()
),请设置响应内容类型 并使用 writer 写入响应数据。编写器会将 UTF-16 编码的字符数据转码为 UTF-8。
From your comment, you imply that you are doing this:
If you get character data (string/char array) from the queue (
txtMsg.getText()
), set the response content type and use a writer to write the response data.The writer will transcode the UTF-16-encoded character data to UTF-8.