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;
        }

    }

  • 相关阅读:
    用css3弹性盒子模型实现九宫格布局
    css布局之左中右结构总结
    内联块inline-block的垂直间隙问题
    Jquery选择器 讲解
    U大师装系统
    陈伟:软件趋势——云物移大智
    asp网站发布步骤总结
    MYfirst
    ASP.net 利用From实现上传文件
    【ASP.NET Core CentOS 发布】(四)在CentOS上安装.NET Core运行时、部署到CentOS
  • 原文地址:https://www.cnblogs.com/shendandan/p/4568512.html
Copyright © 2011-2022 走看看