java 接收 post xml数据_servlet接收xml数据流[java]

我是这么收的

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@]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值