如何解码带有法语符号的消息?MQ中消息的示例:
"name":" ĄĆĘŁŃÓŚŹŻąćęłńóśźżƵƶÁáÂâÃãÀàÇçÉéÊêÍíÓóÔôÕõÚúÀÉÈÍÏÓÒÚÜÇ",
"id":"2463"
当我从MQ收到消息时,有:
"name":" ",
"id":"2463"
我使用的代码:
public static String getMessage() throws JMSException {
String message = null;
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQGMO_ALL_MSGS_AVAILABLE | MQGMO_WAIT | MQGMO_PROPERTIES_AS_Q_DEF | MQGMO_FAIL_IF_QUIESCING | MQOO_INPUT_AS_Q_DEF | MQGMO_SYNCPOINT
| MQC.MQGMO_CONVERT;;
cf.setHostName(HOST);
cf.setChannel(CHANNEL);
cf.setPort(PORT);
cf.setQueueManager(QMN);
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue(QUEUE_NAME);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
connection.start();
TextMessage receivedMessage = (TextMessage) receiver.receive();
byte[] bytes = receivedMessage.getText().getBytes("UTF-8");
String s = new String(bytes, "UTF-8");
TextMessage
的实例,则它将包含符合UTF-16的字符串,而不是字节数组。 但是,您应该添加一些保护逻辑以确保:
Message receivedMessage = receiver.receive();
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
try {
String text = textMessage.getText();
...
} catch (JMSException jmsex) {
...
}
} else {
// Not a TextMessage, maybe its a JMSBytesMessage, let's check
...
}