zoukankan      html  css  js  c++  java
  • ssh接收和返回xml

    Struts中配置:

    <action name="xxx" class="xxxxx" method="xxx">
       <result name="success" type="xslt">
       </result>
      </action>

    action中:

    public String Weixin_Notify() {
      HttpServletRequest request = getHttpServletRequest();
      HttpServletResponse response = ServletActionContext.getResponse();

    Map<String, String> params = PayNotify.parseXml(request
         .getInputStream());//获得xml并解析

       String WEIXIN_RETURN = "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";

    // 把xml字符串写入响应
        byte[] xmlData = WEIXIN_RETURN.getBytes();
        response.setContentType("text/xml;charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setContentLength(xmlData.length);
        PrintWriter os = response.getWriter();
        os.write(WEIXIN_RETURN);

    ······

    return SUCCESS;}

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.List;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;

    public class PayNotify {

    @SuppressWarnings("unchecked")
     public static Map<String, String> parseXml(InputStream inputStream){
         Map<String, String> map = new HashMap<String, String>();
         try{
          // 解析结果存储在HashMap  
          //InputStream inputStream = request.getInputStream();
          // 读取输入流
          SAXReader reader = new SAXReader();
          Document document = reader.read(inputStream);
          // 得到xml根元素
          Element root = document.getRootElement();
          // 得到根元素的所有子节点
          List<Element> elementList = root.elements();
          // 遍历所有子节点
          for (Element e : elementList)
           map.put(e.getName(), e.getText());
          // 释放资源
          inputStream.close();
          inputStream = null;
          System.out.println("获取的微信值:" + map);
          return map;
      }catch (Exception e) {
       e.printStackTrace();
      }
      return null;
        }

    }

  • 相关阅读:
    Android之SurfaceView学习(一)
    AS3的加载机制(带例子) 转载
    Timer与TimerTask的用法
    A*寻路初探 GameDev.net (转载)
    解决在Sql Server2005查询分析器中读取Excel表出现的一些问题
    svn更改用户问题
    MVC4 WEBAPI初探
    使用WORD2013发布blog
    一个IIS中可否支持两个版本的Freamwork
    web打印也能分页
  • 原文地址:https://www.cnblogs.com/shendandan/p/4568512.html
Copyright © 2011-2022 走看看