我是这么收的
BufferedReader reader1 = request.getReader();
InputSource is= new InputSource(reader1);
if(is != null){
System.out.print(readXMLFile(is));
}
else{
System.out.print("no data receive");
}
private String readXMLFile(InputSource in) throws Exception {
Vector student_Vector = new Vector();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
Document doc = null;
try {
doc = db.parse(in);
} catch (DOMException dom) {
System.err.println(dom.getMessage());
System.exit(1);
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
但是收不到
/*********************************************/
public void send(URLConnection connection, String strXml) {
//产生输出流
BufferedOutputStream out = null;
BufferedInputStream bin=null;
try {
out = new BufferedOutputStream(connection.getOutputStream());
String mob = null;
mob = //strXml;
URLEncoder.encode(strXml, "UTF-8");
out.write(mob.getBytes());
out.flush();
//接受服务器response的值
bin = new BufferedInputStream(connection.getInputStream());
int len = bin.available();
System.out.println("len:" + len);
byte[] tmp = new byte[len];
bin.read(tmp);
System.out.println("return value:" + new String(tmp));
}
catch (Throwable t) {
t.printStackTrace();
}
}
这个是发送的
/*****************************************************/
//获取post数据
public String getDoc(HttpServletRequest request) throws Exception {
char[] readerBuffer = new char[request.getContentLength()];
BufferedReader bufferedReader = request.getReader();
logger.info("开始处理上传数据");
int portion = bufferedReader.read(readerBuffer);
int amount = portion;
while (amount < readerBuffer.length) {
portion = bufferedReader.read(readerBuffer, amount,
readerBuffer.length - amount);
amount = amount + portion;
}
StringBuffer stringBuffer = new StringBuffer(
(int) (readerBuffer.length * 1.5));
for (int index = 0; index < readerBuffer.length; index++) {
char c = readerBuffer[index];
stringBuffer.append(c);
}
String xml = stringBuffer.toString();
logger.info(xml);
return xml;
}
/*******************************************************/
获得xml字符串后,使用dom4j parse为DOM对象,就可以了。
Document document = DocumentHelper.parseText(xml);
/****************************************************/
InputStream is=request.getInputStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(is);
/***************************************************/
public String urlDecode(String value) {
try {
return java.net.URLDecoder.decode(value, "GB2312");
}
catch (Exception ex) {
ex.printStackTrace();
}
return value;
}
最后乱码的用这个类搞定了
[@more@]