首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

axis1.4发送接收讯息中文乱码

2011-12-24 
axis1.4发送接收消息中文乱码客户端:String xmlString // ?xml version\1.0\ encoding\UTF-8\?\

axis1.4发送接收消息中文乱码
客户端:
String xmlString =// "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
  "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
  + "<soapenv:Body>"
  + "<LogoutRequest xmlns=\"http://www.MMarket.com/sims/schemas/\">"
  + " <Head>"
  + " <MsgType>中文</MsgType>"
  +" <ActionCode>0</ActionCode>"
  +" <TransactionID>1200908030000001</TransactionID>"
  + " <Version>0001</Version>"
  + " <ProcessTime>20090803142702</ProcessTime>"
  + " <Send_Address>"
  + " <DeviceType>5</DeviceType>"
  + " <DeviceID>8889</DeviceID>"
  + " </Send_Address>"
  + " <Dest_Address>"
  + " <DeviceType>2</DeviceType>"
  + " <DeviceID>8888</DeviceID>"
  + " </Dest_Address>"
  + " <UserId>MobileMarket</UserId>"
  + " <CheckTag>0</CheckTag>"
  + " <ExpectLoadTime>20090803142702</ExpectLoadTime>"
  + " </Head>"
  +" <Body/>"
  + " </LogoutRequest>"
  + "</soapenv:Body>"
  + "</soapenv:Envelope>";

  MessageFactory mf = MessageFactory.newInstance();
   
  SOAPMessage smsg = mf.createMessage();
   
  MimeHeaders headers = smsg.getMimeHeaders();  

  headers.addHeader("Content-Type", "text/xml; charset=gb2312");
   
  smsg = mf.createMessage(headers, new ByteArrayInputStream(xmlString.getBytes()));

  SOAPConnection conn = SOAPConnectionFactory.newInstance().createConnection();

服务端:

public void process(SOAPEnvelope req, SOAPEnvelope resp) throws javax.xml.soap.SOAPException 
  {
  String msg = req.getBody().toString();//这里得到的msg里中文字符就乱码了
}

[解决办法]
转换一下吧

Java code
public String setCharSet(Document document, String charSet)            throws Exception {        OutputFormat format = OutputFormat.createPrettyPrint();        // xml文件字符集为GBK        format.setEncoding(charSet);        ByteArrayOutputStream fos = new ByteArrayOutputStream();        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,                charSet));        XMLWriter writer = new XMLWriter(bw, format);        writer.write(document);        bw.close();        String restr = fos.toString();        fos.close();        return restr;    } 

热点排行