日期:2014-05-20  浏览次数:21002 次

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;
    }