最近遇到用AXIS调用php的web service服务,出现中文编码问题,细察原因,是由于php的服务端采用iso编码,而axis的客户端程序不支持,因此修改了axis的源代码使之支持iso编码。
1、download the axis 1.4 source code;
2、In package org.apache.axis.components.encoding,modify XMLEncodingFactory:
......
public static final String ENCODING_ISO_8859_1="ISO-8859-1";
......
encoderMap.put(ENCODING_ISO_8859_1, new Latin1Encoder());
encoderMap.put(ENCODING_ISO_8859_1.toLowerCase(), new Latin1Encoder());
......
3、Add class Latin1Encoding:
package org.apache.axis.components.encoding;
import java.io.IOException;
import java.io.Writer;
import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;
/**
* Latin1 Encoder.
*
* @author <a href="mailto:jens@void.fm">Jens Schumann</a>
* @see <a href="http://encoding.org">encoding.org</a>
*/
class Latin1Encoder extends AbstractXMLEncoder {
/**
* gets the encoding supported by this encoder
*
* @return string
*/
protected static Log log =
LogFactory.getLog(Latin1Encoder.class.getName());
public String getEncoding() {
return XMLEncode***ctory.ENCODING_ISO_8859_1;
}
/**
* write the encoded version of a given string
*
* @param writer writer to write this string to
* @param xmlString string to be encoded
*/
public void writeEncoded(Writer writer, String xmlString)
throws IOException {
if (xmlString == null) {
return;
}
/*
int length = xmlString.length();
char character;
for (int i = 0; i < length; i++) {
character = xmlString.charAt( i );
writer.write(character);
}
*/
writer.write(new String(xmlString.getBytes(),getEncoding()));
//log.debug("encoding string with latin1:"+xmlString+" length="+length);
//log.debug("encoding result:"+result);
}
}