package cn.lihainan.test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class Demo1 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try { // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
request.setCharacterEncoding("utf-8");
response.setContentType("textml;charset=utf-8");
InputStream inputStream = request.getInputStream();
String result = readFromInputStream(inputStream);
PrintWriter out = response.getWriter();
out.println("原始消息:");
out.println(result);
//解析 建立document解析
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
InputStream xmlStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
Document document = builder.parse(xmlStream);
//定义节点 根据节点从document中找到要输出的
Node ToUserName = document.getElementsByTagName("ToUserName").item(0);
System.out.println(ToUserName.getTextContent());
Node FromUserName = document.getElementsByTagName("FromUserName").item(0);
System.out.println(FromUserName.getTextContent());
Node MsgType= document.getElementsByTagName("MsgType").item(0);
System.out.println(MsgType.getTextContent());
Node Content = document.getElementsByTagName("Content").item(0);
System.out.println(Content.getTextContent());
Node MsgId= document.getElementsByTagName("MsgId").item(0);
System.out.println(MsgId.getTextContent());
out.println();
//输出
out.println("解析结果:");
out.print("FromUserName(来自):");
out.println(FromUserName.getTextContent());
out.print("ToUserName(发送至):");
out.println(ToUserName.getTextContent());
out.print("CreateTime(时间):");
Node createTime= document.getElementsByTagName("CreateTime").item(0);
int t = Integer.parseInt(createTime.getTextContent());
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
out.println(format.format((long)t*1000L));
out.print("MsgType(消息类型):");
out.println(MsgType.getTextContent());
out.print("Content(文本内容):");
out.println(Content.getTextContent());
out.print("MsgId(文件消息标识号):");
out.println(MsgId.getTextContent());
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将输入流InputStream变为String
public String readFromInputStream(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
in.close();
// 解析内容 看网上更改 输出中文乱码 但是未解决 已知输入流为gbk 输出为utf-8 求如何转换
byte[] lens = baos.toByteArray();
String result = new String(lens,"UTF-8");
return result;
}
}