Java 迷失在翻译编码中
我目前正在尝试处理从J2me应用程序到Websphere 6服务器的http post请求,该服务器通过MQ消息传递与其他系统通信。队列字符集为Cp037,从队列检索到的消息为Cp1256 在我尝试用阿拉伯语向j2me客户端发送响应之前,一切正常:Java 迷失在翻译编码中,java,encoding,java-me,utf-8,websphere,Java,Encoding,Java Me,Utf 8,Websphere,我目前正在尝试处理从J2me应用程序到Websphere 6服务器的http post请求,该服务器通过MQ消息传递与其他系统通信。队列字符集为Cp037,从队列检索到的消息为Cp1256 在我尝试用阿拉伯语向j2me客户端发送响应之前,一切正常: String respStr = new String(orginalMsg, "Cp1256"); response.getOutputStream().write(bytes); servlet的响应设置为: 字符编码:UTF-8 内容类型
String respStr = new String(orginalMsg, "Cp1256");
response.getOutputStream().write(bytes);
servlet的响应设置为:
- 字符编码:UTF-8
- 内容类型:文本/普通;字符集=UTF-8
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阿拉伯语字符,因为我可以在表单上显示harcoded字符串
但是,当只显示响应的结果时?将显示字符
我尝试以编程方式进行转换,而不是让Websphere进行隐式转换,但我得到了相同的结果
任何提示都将不胜感激。从您的评论中,您暗示您正在这样做:
//these bytes are Cp037 encoded
bytes = txtMsg.getText().getBytes("Cp037");
//you don't seem to be using respStr
String respStr = new String(orginalMsg, "Cp1256");
//you've told this response it is UTF-8, but the bytes are Cp037
response.getOutputStream().write(bytes);
如果从队列(txtMsg.getText()
)中获取字符数据(字符串/字符数组),请设置响应并使用来写入响应数据
response.setContentType("text/plain;charset=UTF-8");
response.getWriter(txtMsg.getText());
作者将把UTF-16编码的字符数据转换成UTF-8。这里有一篇漂亮的文章可以帮助你——我昨天刚刚看了。请告诉我们第一个代码片段中的
bytes
来自哪里?bytes来自JMS TextMessage:bytes=txtMsg.getText().getBytes(“Cp037”);